dongliechuich10319 2017-08-18 14:52
浏览 43
已采纳

Symfony - Sonata Abstract Admin和getDoctrine

I want to use the prePersist() hook to set a fetched object into a ready-to-be-persisted object. But I can't figure how to use doctrine with Sonata Admin Bundle.

Here is my 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 anyone have any idea how to do it ?

Thanks !

  • 写回答

1条回答 默认 最新

  • duanpai1033 2017-08-21 07:39
    关注

    Okay guys, so I have figured it out !

    I had to specify the orm default entity manager in my service argument

    #app/config/services.yml
    
    arguments: [~, ShareBundle\Entity\UserShare, ~, @doctrine.orm.default_entity_manager]
    

    And I had to extend the construct function in my Admin class.

     public function __construct($code, $class, $baseControllerName, $em)
    {
        parent::__construct($code, $class, $baseControllerName);
        $this->em = $em;
    }
    

    (Thanks to this answer)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 CST仿真别人的模型结果仿真结果S参数完全不对
  • ¥15 pyqt5读取ui文件报错
  • ¥15 请问在阿里云服务器中怎么利用数据库制作网站
  • ¥60 ESP32怎么烧录自启动程序
  • ¥50 html2canvas超出滚动条不显示
  • ¥15 java业务性能问题求解(sql,业务设计相关)
  • ¥15 52810 尾椎c三个a 写蓝牙地址
  • ¥15 elmos524.33 eeprom的读写问题
  • ¥15 用ADS设计一款的射频功率放大器
  • ¥15 怎么求交点连线的理论解?