Reputation: 2126
I'm building an application in Symfony 2.2.
I use a custom repository class for one of my entities. I include it like this:
@ORM\Entity(repositoryClass="MyApp\MainBundle\Entity\CategoryRepository")
I want to use methods on my category entites from two other repository classes.
@ORM\Entity(repositoryClass="Gedmo\Sortable\Entity\Repository\SortableRepository")
@ORM\Entity(repositoryClass="Gedmo\Tree\Entity\Repository\NestedTreeRepository")
The entity, however, only expects one repositoryClass. How can I use all three of these repositories for the same entity?
Upvotes: 1
Views: 2046
Reputation: 933
I believe, you can only have one repository per Entity.
So you have referenced
ORM\Entity(repositoryClass="MyApp\MainBundle\Entity\CategoryRepository")
If you must use another repositories within the repository,
You can get the EntityManager for those Repositories (if not same) eg: $em2 = whatEverIsTheEntity()
Now you can have proxy methods in CategoryRepository to the methods in the repository you want to use. eg: $em2->getMethodFromEm2()
.
Note: But I wouldn't prefer above solution unless there is really a need. I would rather create a service layer which has Doctrine Entity Manager as Dependency Injected. This service layer will connect to all the repositories and collect final result whatever you want to achieve (to be used in the controller). This way, the code will be much cleaner and testable as well.
Hope this helps.
Upvotes: 2
Reputation: 29912
Repositories are class (so entity) reserved. If you inherit from super classes (even if those classes doesn't inherit directly, but repos can), than you can make your repository (CategoryRepository
) extended by other two (one extend the other, since PHP doesn't allow more than one class extension).
Pretty much something like that
class NestedTreeRepository extends SortableRepository
{
}
class CategoryRepository extends NestedTreeRepository
{
}
don't know if this fits your needs, but this a way (and I'll not surprised if is the only one) for realize what you're trying to do here
Upvotes: 1