Injecting repositories into Factory Symfony2

[亡魂溺海] 提交于 2019-12-13 07:44:02

问题


I am learning concept of service injection in Symfony2 framework. I have this set up. Repository, Factory, Controller. I am trying to inject repository into a factory to create objects for my controller to handle.

I set up a services.xml file where I am trying to declare my service and i guess this is where i am going wrong.

Fatal error: Uncaught exception 'Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException' with message 'The service "joint.venture.postcode.factory" has a dependency on a non-existent service

My repository:

class Postcode {

    private $postcode;
    private $paf;

    public function setPostcode($postcode)
    {
        $this->postcode = $postcode;
    }

    public function getPostcode()
    {
        return $this->postcode;
    }

    public function setPaf($paf)
    {
        $this->paf = $paf;
    }

    public function getPaf()
    {
        return $this->paf;
    }
} 

My Factory

use Test\Bundle\Repository\Postcode;

class PostcodeFactory
{

    private $postcode;

    public function __construct(
        Postcode $postcode
    ){
        $this->postcode = $postcode;
    }

    public function test()
    {
        return $this->setPostcode('Hello');
    }

} 

my services:

<service id="test.postcode.factory"
         class="Test\Bundle\Factory\PostcodeFactory">

        <argument type="service" id="repository.postcode"/>
</service>

Anayone sees something wrong..?


回答1:


repository.postcode does not exist as a service.

Generally, it's a little tricky to do that, because Repos are getted from the EntityManager

I generally prefer to inject the EM, and then ask it for the repos




回答2:


You need to register your repository as a service, before you can inject it in another service.

<service id="repository.postcode"
    class="Test\Bundle\Repository\Postcode">
        <factory service="doctrine.orm.entity_manager" method="getRepository" />
        <argument>Test\Bundle\Entity\Postcode</argument>
</service>



回答3:


Oops. Looks like @Gerry beat me to it. And he uses xml. Oh well.

Here is an example of defining a repository as a service. As with most things, it's straight forward once you have done a few. I use yaml instead of xml but the concept is the same. I also like to alias the entity manager name but it's not required.

cerad_user.entity_manager.doctrine:
    alias: doctrine.orm.default_entity_manager

cerad_user.user_repository.doctrine:
    class:  Cerad\Bundle\UserBundle\Entity\UserRepository
    factory_service: 'cerad_user.entity_manager.doctrine'
    factory_method:  'getRepository'
    arguments:  
        - 'Cerad\Bundle\UserBundle\Entity\User'

If all your service needs is a repository then injecting a repository is cleaner than injecting the entire entity manager. At least in my not so humble opinion.



来源:https://stackoverflow.com/questions/29672519/injecting-repositories-into-factory-symfony2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!