doctrine / DoctrineMongoDBBundle

Integrates Doctrine MongoDB ODM with Symfony
http://symfony.com/doc/current/bundles/DoctrineMongoDBBundle/index.html
MIT License
382 stars 230 forks source link

Autowiring for repository classes #443

Closed xorik closed 6 years ago

xorik commented 6 years ago

The ORM bundle have a helper class to make repositoires autowired. The class is ServiceEntityRepository. Example is here.

It would be great, if ODM bundle have the same for the DocumentRepository class.

I think something like this should work:

use Doctrine\ODM\MongoDB\DocumentManager;
use Doctrine\ODM\MongoDB\DocumentRepository;

class ServiceDocumentRepository extends DocumentRepository
{
    public function __construct(DocumentManager $dm, string $documentClass)
    {
        parent::__construct($dm, $dm->getUnitOfWork(), $dm->getClassMetadata($documentClass));
    }
}
alcaeus commented 6 years ago

@michaelperrin is currently working on porting the solution from DoctrineBundle to this bundle: https://github.com/doctrine/DoctrineMongoDBBundle/pull/441#issuecomment-348694857

michaelperrin commented 6 years ago

I confirm I started working on it, and I should be able to open a PR this week.

seqq commented 6 years ago

Sorry ) No news about PR?

michaelperrin commented 6 years ago

@seqq I am sorry I am still working on it and it should be ready soon!

seqq commented 6 years ago

great to hear, thank you!

xorik commented 6 years ago

@seqq you can use my code, just put it in your namespace. It works perfectly

malarzm commented 6 years ago

@michaelperrin any news? :)

michaelperrin commented 6 years ago

@malarzm I am so sorry for the delay, life took over this ;) I could push a WIP PR if needed, which is a port of the ORM counterpart, but which is not finished yet. I keep you posted by the end of the week.

malarzm commented 6 years ago

@michaelperrin no worries at all! Please push the WIP, if you could highlight what is left to do maybe someone will be able to help :)

michelepatrassi commented 6 years ago

made it work following https://github.com/doctrine/DoctrineMongoDBBundle/pull/448/files and the code of @xorik

michaelperrin commented 6 years ago

@malarzm Sorry again for the delay, here is the WIP PR: #449 . I hope to work on it again soon!

back-2-95 commented 6 years ago

Is this related to the exception I'm getting:

Cannot autowire service "App\Repository\TestRepository": argument "$uow" of method "Doctrine\ODM\MongoDB\DocumentRepository::__construct()" references class "Doctrine\ODM\MongoDB\UnitOfWork" but no such service exists.

I'm using Symfony 4 with DoctrineMongoDBBundle and trying to set up a simple Document + RepositoryClass using annotations.

alcaeus commented 6 years ago

Is this related to the exception I'm getting

Yes. ODM repositories are not suited for autowiring (yet).

alcaeus commented 6 years ago

See #473.