[英]How to access Entity class from Sonata Admin's Admin class?
Let me explain: I have an Entity class Item with a method getName() in it, namespace App\\Entity. 让我解释一下:我有一个Entity类Item,其中包含getName()方法,命名空间App \\ Entity。 I have an admin class for it in Sonata Admin, namespace App\\Admin, and would like to call that method from within, how to do so?
我在Sonata Admin,命名空间App \\ Admin中有一个管理类,并且想从内部调用该方法,怎么做?
//Symfony2 Entity
...
class Item
{
public function getName(){
return $this->name;
}
}
...
//Sonata Admin class
class ItemAdmin extends Admin
{
...
protected function configureListFields(ListMapper $listMapper){
//how to access Item class' getName() method from here?
}
}
EDIT: This works inside configureListFields(), but what about without find() and if with find() only, then how to automatically get 'id'? 编辑:这在configureListFields()内部工作,但如果没有find()和如果只有find(),那么如何自动获取'id'?
$item=$this->getConfigurationPool()->getContainer()->get('Doctrine')->getRepository('AppBundle:Item')->find('15');
echo $item->getName();
Simple and easy 简单易行
$container = $this->getConfigurationPool()->getContainer();
$em = $container->get('doctrine.orm.entity_manager');
You have to get EntityManager: 你必须得到EntityManager:
//Sonata Admin class
class ItemAdmin extends Admin
{
...
protected function configureListFields(ListMapper $listMapper){
$id = $this->getSubject()->getId();
$em = $this->modelManager->getEntityManager(YourBundle:Item);
$item = $em->getRepository('YourBundle:Item')->find($id);
$item->getName()
...
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.