Reputation: 2617
I've come across two methods to inject dependencies into custom Doctrine Repositories.
I understand method one in that it no longer uses the Entity Manager to retrieve the repository and instead uses the service manager.
However, I do not understand method two. There are two parts I do not understand. The first is the "Configuration" section. What does the Doctrine configuration key "repository_factory" do?
return array(
'doctrine' => array(
'configuration' => array(
'orm_default' => array(
'repository_factory' => 'Db\Repository\RepositoryFactory',
),
),
),
'service_manager' => array(
'invokables' => array(
'Db\Repository\RepositoryFactory' => 'Db\Repository\RepositoryFactory',
),
),
);
Then second, I am not sure how to retrieve the repository. Am I still supposed to use the entity manager? Or should I use the service manager as I do in method one?
Upvotes: 1
Views: 366
Reputation: 13167
As far as I understand it, the second link gives a way to override the default arguments passed to the Repository when it is instantiated.
The primary role of a factory (not only in Symfony, it's a common design pattern) is to create instances of one or more classes, rather than instantiate them by yourself through the new
keyword.
When you call $em->getRepository('EntityFQCN')
, Doctrine (through the repository_factory
) looks for an eventual Repository class defined in your Entity (i.e. @ORM\RepositoryClass(...)
) and return an instance of it, otherwise, if there is no Repository class found for the entity, it returns an instance of the default EntityRepository, with the specified entity as argument (i.e. getRepository('EntityFQCN')
).
So, without looking at the link in detail, I think I can say that if you try to implement this alternative, you'll retrieve your repository through the "default" way, i.e. EntityManager::getRepository('xxxx')
.
The role of this configuration part will be to instantiate the Repository differently than the default one.
I already overridden the RepositoryFactory when I needed to override the default EntityRepository
(return an instance of a custom Repository class without define it in my entities, in other words replace the default Repository class).
You can see it in action.
Hope you have a lot of fun with DI in Doctrine.
Upvotes: 1