我有一个简单的问题。Symfony2查询配置
我是否应该将使用entitymanager进行的查询放在控制器中,还是应该制作单独的类然后用服务调用它?
protected $entityManager;
protected $securityContext;
public function __construct(EntityManager $entityManager, SecurityContext $securityContext) {
$this->entityManager = $entityManager;
$this->securityContext = $securityContext;
}
public function getTest() {
$query = $this->entityManager->getRepository('PdbTestLoginBundle:PdbDomain')->find(972);
if (!empty($query)) {
return $query;
} else {
return false;
}
}
然后我注册这样的服务::控制器外查询的
例如
`参数: test.controller:PDB \ TestLoginBundle \类\测试
服务: test.defaultcontroller: class:“%test.controller%” arguments: entityManager:“@ doctrine.orm.entity_m anager” 的SecurityContext: “@ security.context”
然后我把它的控制器:
`$帮手= $这个 - >获取( 'test.defaultcontroller'); var_dump($ helper-> getTest());
这是正确的方式还是应该直接进行查询???
您应该将查询构建器放入Repository类中,并告诉Entity类使用您的Repository类 - 这意味着$ em-> getRepository('Bundle:Entity')为您提供了新的Repository类的对象。 – 2014-09-11 11:41:47
所以我需要为每个实体生成一个存储库类? – Tommie 2014-09-11 11:42:41
感谢您的回答! – Tommie 2014-09-11 11:44:07