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 !

图片转代码服务由CSDN问答提供 功能建议

我想使用prePersist()钩子将一个取出的对象设置为一个准备持久的对象 。 但是我无法想象如何在Sonata Admin Bundle中使用doctrine。

这是我的代码:

  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扩展了AbstractAdmin 
 {
 protected function configureFormFields(FormMapper $ formMapper)
 {
 $ formMapper-> add('quantity','text')
  - > add('user','sonata_type_model_list'  ); 
} 
 
受保护的函数configureDatagridFilters(DatagridMapper $ datagridMapper)
 {
 $ datagridMapper-> add('quantity'); 
} 
 
 protected function configureListFields(ListMapper $ listMapper)\  n {
 $ listMapper-> addIdentifier('quantity') - > addIdentifier('user') - > addIdentifier('date'); 
} 
 
公共函数prePersist($ object)
  {
 $ shareManager = $ this-> getDoctrine() - &gt  ; getManager() - > getRepository('ShareBundle:Share'); 
 $ value = $ shareManager-> findOneBy(array(),array('date'=>  'DESC')); 
 $ object-> setShare($ value); 
} 
} 
   
 
 

有没有人知道怎么做 它?

谢谢!

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

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)

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题