php - Symfony - Sonata Abstract Admin and getDoctrine -
i want use prepersist() hook set fetched object ready-to-be-persisted object. can't figure how use doctrine sonata admin bundle.
here code :
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); } }
does have idea how ?
thanks !
okay guys, have figured out !
i had specify orm default entity manager in service argument
#app/config/services.yml arguments: [~, sharebundle\entity\usershare, ~, @doctrine.orm.default_entity_manager]
and had extend construct function in admin class.
public function __construct($code, $class, $basecontrollername, $em) { parent::__construct($code, $class, $basecontrollername); $this->em = $em; }
(thanks this answer)
Comments
Post a Comment