Update documentation to explain service repositories
The documentation at https://symfony.com/doc/current/bundles/DoctrineMongoDBBundle/index.html doesn't contain a section on service repositories, which should definitely be added.
About that, what's the difference between ServiceRepository and "basic" Repository ?
In my case I managed to have autowired Repositories by doing the following:
class ClassRepository extends DocumentRepository
{
public function __construct(DocumentManager $documentManager)
{
$uow = $documentManager->getUnitOfWork();
$classMetadata = $documentManager->getClassMetadata(Class::class);
parent::__construct($documentManager, $uow, $classMetadata);
}
}
As the DocumentManager is autowirable it's quite easy to do.
I can then inject the Repository in my Services.
Nothing - the ServiceDocumentRepository class is just a nice layer that hides these internals from you so that you only need to call the parent constructor:
final class FooRepository extends ServiceDocumentRepository
{
public function __construct(ManagerRegistry $registry)
{
parent::__construct($registry, Foo::class);
}
}
Not entirely nothing, injecting DocumentManager will break as soon as you have more than 1 manager configured I believe.
I never had the case with multiple managers yet.
In that case how the ManagerRegistry "choose" the right existing Manager ?
With each manager's mapping configuration you're setting which documents are managed by a document manager. Although I believe it's not enforced, one document should not be managed by two separate document managers. This way given a class, ManagerRegistry will find a proper document manager for it.
Oh yeah I missed that part as I didn't need that.
Thanks a lot for the explanation.
One more thing: when fetching the repository from the container you will receive a different instance from the one you receive from $documentManager->getRepository(). When you use the actual service repositories, we inject the service you declared into a special repository factory so you'll receive the actual service instance when calling getRepository().