[英]Injecting repositories into Factory Symfony2
I am learning concept of service injection in Symfony2 framework. 我正在学习Symfony2框架中服务注入的概念。 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. 我在试图声明我的服务的地方设置了services.xml文件,我想这就是我要出错的地方。
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..? Anayone看到了什么问题..?
repository.postcode
does not exist as a service. repository.postcode
不作为服务存在。
Generally, it's a little tricky to do that, because Repos are getted from the EntityManager
通常,这样做有点棘手,因为Repos是从
EntityManager
I generally prefer to inject the EM, and then ask it for the repos 我通常更喜欢注入EM,然后要求其回购
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>
Oops. 哎呀。 Looks like @Gerry beat me to it.
看起来@Gerry击败了我。 And he uses xml.
而且他使用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.
我使用yaml而不是xml,但是概念是相同的。 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.
至少在我看来并不那么卑鄙。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.