【发布时间】:2014-04-04 11:32:40
【问题描述】:
我想在奏鸣曲管理列表网格中添加一个按钮,具体取决于对象是否满足条件。
我有一个带有此代码的自定义类型模板:
{% if object.myMedhod == true %}
///if response is true add a button in sonata admin list grid
{% endif %}
我的实体扩展了我拥有 myMedhod() 的模型类
我的模型类:
namespace myprject/myBundle/Model;
use Doctrine\ORM\EntityManager;
class XModel
{
private entityManager;
public function __constuct(EntityManager $entityManager)
{
$this->entityManager = $entityManager;
}
public function myMethod()
{
$this->entityManager->getDoctrine()->getRepostiroy(xxx)->find(xxx)
}
}
我的服务:
services:
my_service:
class: myProject/myBundle/Model/XModel
arguments:
- @doctrine.orm.entity_manager
我认为我的代码中的所有内容都是正确的,但是我在获取 entityManager 时遇到了问题......似乎如果不调用类中的构造方法!
抛出 symfony 的 bug 是:
在非对象上调用成员函数 getDoctrine()
/////////////////////////////////////// /////////////////////
好的,我已经解决了改变方法的问题!
在 twig 模板中使用对象并调用模型类中的方法,
我现在调用 adminClass 中的 a 方法并传递对象:
{% if admin.myMedhod(object) == true %}
{% endif %}
然后在服务表单中我添加了 adminClass
调用: - [setSecurityContext, [@security.context]] - [setEntityManager , [@doctrine.orm.entity_manager]]
在管理类中:
public function setEntityManager($entityManager) {
$this->entityManager = $entityManager;
}
public function getEntityManager() {
return $this->entityManager;
}
public function myMedhod($object){
$condition=$this->getEntityManager()->getRepository(xxx)->findOneBy(array("field"=>$object->getId()));
if(empty($condition)){
return true;
}else{
return false;
}
}
感谢大家的帮助
【问题讨论】:
-
为什么要将实体管理器注入实体类?
-
我不想...我想在一个由实体扩展的类中进行。从模板访问他的方法并对数据库进行查询。
-
你的模型
myprject/myBundle/Model/XModel类负责什么?
标签: symfony entitymanager sonata-admin