Skip to content

Commit

Permalink
Replaced section on injecting repositories
Browse files Browse the repository at this point in the history
  • Loading branch information
TomHAnderson committed Oct 25, 2024
1 parent 21d4f41 commit c4d9b5d
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 46 deletions.
14 changes: 7 additions & 7 deletions docs/multiple-connections.rst
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,16 @@ sets of entities. In other words, one entity manager that connects to one
database will handle some entities while another entity manager that connects
to another database might handle the rest.

The default manager is configured in ``doctrine.managers``. This one will get
used when you use the ``EntityManager`` directly. You can add more managers
to this array.
The default manager is configured in ``doctrine.managers`` and is
called ``default``. This one will get used when you use the ``EntityManager``
directly. You can add more managers to this array.

In your application, you can inject
``Doctrine\Common\Persistence\ManagerRegistry``. This holds all entity managers.
``ManagerRegistry@getManager()`` will return the default manager. By passing
through the manager name, you will get the connection you want. Alternatively
you can use ``getManagerForClass($entityName)`` to get a manager which is
suitable for that entity.
``$managerRegistry->getManager()`` will return the default manager. By passing
through the manager name, such as default, you will get the connection you
want. Alternatively you can use ``getManagerForClass($entityName)`` to get a
manager which is suitable for that entity.


.. role:: raw-html(raw)
Expand Down
75 changes: 36 additions & 39 deletions docs/repositories.rst
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ write collecting logic, build queries, etc.

Repositories are usually modeled as collections to abstract away persistence
lingo, so it is very common to see methods
like ``find($id)``, ``findByName("Patrick")``, as if your entities would be in
a ``Collection`` object instead of a database.
like ``findByName("Lasso")``, treating the repository as a collection.

Doctrine comes with a generic ``Doctrine\Common\Persistence\ObjectRepository``
interface that lets you easily find one,
Expand All @@ -20,58 +19,56 @@ and an implementation of it in ``Doctrine\ORM\EntityRepository``.
Getting a repository instance
=============================

The easiest way to get a repository is to let the EntityManager generate one
The easiest way to get a repository is to let the EntityManager provide one
for the Entity you want:

.. code-block:: php
$repository = EntityManager::getRepository(Scientist::class);
This will generate an instance of the `Doctrine\ORM\EntityRepository`, a
generic implementation ready to be queried for the class that was given to it.

Injecting repositories
======================
.. code-block:: php
You can inject generic repositories by using Laravel's
`contextual binding <https://laravel.com/docs/container#contextual-binding>`_.
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
.. code-block:: php
#[ORM\Entity(repositoryClass: "App\Doctrine\ORM\Repository\ScientistRepository")]
class Scientist
{
#[ORM\Id]
#[ORM\Column(type: "integer")]
#[ORM\GeneratedValue(strategy: "AUTO")]
private int $id;
namespace App\Entities\Research;
#[ORM\Column(type: "string", nullable: false)]
private string $firstName;
use Doctrine\Common\Persistence\ObjectRepository;
#[ORM\Column(type: "string", nullable: false)]
private string $lastName;
class Laboratory
{
/**
* @var ObjectRepository
*/
private $scientists;
#[ORM\OneToMany(targetEntity: Theory::class, mappedBy: "scientist")]
private Collection $theories;
public function __construct(ObjectRepository $scientists)
public function __construct()
{
$this->scientists = $scientists;
$this->theories = new ArrayCollection();
}
}
// Then, in one of your ServiceProviders
use App\Entities\Research\Laboratory;
use App\Entities\Research\Scientist;
use Doctrine\Common\Persistence\ObjectRepository;
$repository = EntityManager::getRepository(Scientist::class);
class AppServiceProvider
{
public function register()
{
$this->app
->when(Laboratory::class)
->needs(ObjectRepository::class)
->give(function(){
return EntityManager::getRepository(Scientist::class);
});
}
}
This will return an instance of the ``App\Doctrine\ORM\Repository\ScientistRepository``.
For entities that do not have a repositoryClass, an instnace of
the config ``reposiotry`` repository class is returned.


Injecting repositories
======================

Injecting repositories is **not** recommended. Inject the entity manager
instead and always use it as a container for repositories.

The entity manager is both an entity manager and a container similar to
PSR-11. Instead of injecting repositories, take a step back and be comfortable
injecting the container (entity manager).


Extending repositories
Expand Down

0 comments on commit c4d9b5d

Please sign in to comment.