is it possible to have two repositories for the same entity ?
I try something like that, but it does not work..
class PackageRepository extends EntityRepository
{
public function __construct($em, Mapping\ClassMetadata $class)
{
$cmf = $em->getMetadataFactory();
$class = $cmf->getMetadataFor('Product');
parent::__construct($em, $class);
}
}
Any ideas ?
First of all, why would you want to do that?
Second, to answer your question. You can have as many repositories working with same entities as you like, they are just simple classes after all.
But you can link only one class with the entity class using the @Repository annotation (or YAML, or XML, whatever). All the mapping data is stored in the EntityManager. EntityManager will know only one repository class is linked with the entity class, if you try to get it with $entity->getReposiotry()
or similar it will return only the linked class.
But nothing can stop you from creating your own classes which do some queries and call them directly, explicitly, without relying on the EntityManagers repository mapping.
I also have run into the same issue, when two Symfony bundles work with same entity, but queries are different for each bundle, so I also decided create separate repository for each bundle. For Doctrine 2 the solution can be:
In code:
$myEntityRepo = new MyEntityRepository($entityManager, $entityManager->getClassMetadata('AppBundle:MyEntity'));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With