DoctrineMongoDBBundle icon indicating copy to clipboard operation
DoctrineMongoDBBundle copied to clipboard

Update documentation to explain service repositories

Open alcaeus opened this issue 7 years ago • 7 comments

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.

alcaeus avatar Dec 26 '18 15:12 alcaeus

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.

etshy avatar Dec 27 '18 04:12 etshy

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);
    }
}

alcaeus avatar Jan 02 '19 13:01 alcaeus

Not entirely nothing, injecting DocumentManager will break as soon as you have more than 1 manager configured I believe.

malarzm avatar Jan 02 '19 18:01 malarzm

I never had the case with multiple managers yet.

In that case how the ManagerRegistry "choose" the right existing Manager ?

etshy avatar Jan 02 '19 19:01 etshy

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.

malarzm avatar Jan 02 '19 21:01 malarzm

Oh yeah I missed that part as I didn't need that.

Thanks a lot for the explanation.

etshy avatar Jan 02 '19 21:01 etshy

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().

alcaeus avatar Jan 03 '19 10:01 alcaeus