我想使用prePersist()钩子将一个获取的对象设置为一个可以持久化的对象。但我无法想象如何在Sonata Admin Bundle中使用学说。
这是我的代码:
namespace ShareBundle\Admin;
use Sonata\AdminBundle\Admin\AbstractAdmin;
use Sonata\AdminBundle\Datagrid\ListMapper;
use Sonata\AdminBundle\Datagrid\DatagridMapper;
use Sonata\AdminBundle\Form\FormMapper;
class UserShareAdmin extends AbstractAdmin
{
protected function configureFormFields(FormMapper $formMapper)
{
$formMapper->add('quantity', 'text')
->add('user', 'sonata_type_model_list');
}
protected function configureDatagridFilters(DatagridMapper $datagridMapper)
{
$datagridMapper->add('quantity');
}
protected function configureListFields(ListMapper $listMapper)
{
$listMapper->addIdentifier('quantity')->addIdentifier('user')->addIdentifier('date');
}
public function prePersist($object)
{
$shareManager = $this->getDoctrine()->getManager()->getRepository('ShareBundle:Share');
$value = $shareManager->findOneBy(array(), array('date' => 'DESC'));
$object->setShare($value);
}
}
有没有人知道怎么做?
谢谢!
答案 0 :(得分:1)
好的伙计们,所以我已经弄清楚了!
我必须在服务参数
中指定orm默认实体管理器#app/config/services.yml
arguments: [~, ShareBundle\Entity\UserShare, ~, @doctrine.orm.default_entity_manager]
我必须在Admin类中扩展构造函数。
public function __construct($code, $class, $baseControllerName, $em)
{
parent::__construct($code, $class, $baseControllerName);
$this->em = $em;
}
(感谢this answer)