symfony2中文手册_Symfony2 在service中如何使用Doctrine

更新时间:2019-08-07    来源:Symfony    手机版     字体:

【www.bbyears.com--Symfony】

在service中有个方法需要调用Doctrine,使用

 代码如下 $user = $this->getDoctrine()->getRepository("AtotrukisMainBundle:User")

提示"getDoctrine"找不到,那么我们应该怎么做呢?

解决方法:

第一步:

在service配置文件中添加arguments

 代码如下


services:

    eventService:

        class: Atotrukis\MainBundle\Service\EventService

        arguments: ["@doctrine.orm.entity_manager"]

第二步:

在service对应的class:Atotrukis\MainBundle\Service\EventService中增加如下代码:

 

 代码如下

protected $em;

public function __construct($em)

{

    $this->em = $em;

}

本文来源:http://www.bbyears.com/jiaocheng/60671.html