Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update documentation to explain service repositories #516

Open
alcaeus opened this issue Dec 26, 2018 · 7 comments
Open

Update documentation to explain service repositories #516

alcaeus opened this issue Dec 26, 2018 · 7 comments
Labels
Documentation Hacktoberfest Good issue for participating in Hacktoberfest
Projects

Comments

@alcaeus
Copy link
Member

alcaeus commented Dec 26, 2018

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.

@etshy
Copy link

etshy commented Dec 27, 2018

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.

@alcaeus
Copy link
Member Author

alcaeus commented Jan 2, 2019

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

@malarzm
Copy link
Member

malarzm commented Jan 2, 2019

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

@etshy
Copy link

etshy commented Jan 2, 2019

I never had the case with multiple managers yet.

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

@malarzm
Copy link
Member

malarzm commented Jan 2, 2019

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.

@etshy
Copy link

etshy commented Jan 2, 2019

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

Thanks a lot for the explanation.

@alcaeus
Copy link
Member Author

alcaeus commented Jan 3, 2019

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 alcaeus added this to 4.1 in Roadmap Jan 21, 2019
@alcaeus alcaeus moved this from 4.1 to 3.6 in Roadmap Jan 21, 2019
@malarzm malarzm added the Hacktoberfest Good issue for participating in Hacktoberfest label Sep 24, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Documentation Hacktoberfest Good issue for participating in Hacktoberfest
Projects
Roadmap
3.6 (unsupported)
Development

No branches or pull requests

4 participants