我在symfony 2.0中实现了多个数据库概念。现在我需要在依赖注入概念的myservice.php文件中动态获取实体管理器。我怎么称呼这个实体经理?
services.xml:
<container xmlns="http://symfony.com/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com /schema/dic/services/services-1.0.xsd">
<services>
<service id="my_service" class="Traxcrm\SalesBundle\Services\MyService">
<argument type="service" id="doctrine" />
</service>
</services>
</container>
mysevice.php
class MyService {
private $doctrine;
public function __construct(Doctrine\Bundle\DoctrineBundle\Registry $doctrine)
{
$this->doctrine = $doctrine;
}
class MyService {
private $doctrine;
public function __construct(Doctrine\Bundle\DoctrineBundle\Registry $doctrine) {
$this->doctrine = $doctrine;
}
public function getUserDetails($id) {
$query = $this->doctrine->getEntityManager('Test')->createQuery("SELECT p FROM TraxcrmSigninBundle:Tblallusers p where p.id=$id");
$userDetails = $query->getArrayResult();
return $userDetails;
}
答案 0 :(得分:0)
设置服务并注入学说:
服务定义(services.yml或config.yml):
services:
service_name:
class: <namespace>myservice.php
arguments: ['@doctrine']
或xml:
<services>
<service id="service_name" class="<namespace>myservice.php">
<argument type="service" id="doctrine"/>
</service>
</services>
然后设置你的类来接受传递的学说对象
public class myservice {
private $doctrine;
public function __construct(Doctrine\Bundle\DoctrineBundle\Registry $doctrine) {
$this->doctrine = $doctrine;
}
public function aFunction() {
$em = $this->doctrine->getEntityManager('<name>')......
}
}