本文介绍了SonataAdminBundle 表单字段查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在管理类的 SonataAdminBundle 中,我无法在 ManyToMany 字段上创建 orderBy.

In SonataAdminBundle in Admin class I cannot make an orderBy on ManyToMany field.

例如作者和书籍.作者可以有很多书,书也可以有很多作者.在上面的链接中写道,我可以对表单字段使用查询.所以我可以准备一个查询来选择作者并按姓名排序.如何管理?如何获取 EntityManager 以创建查询并通过查询选项传递它?

For example Author and Book. Author can have many books, as well as Book can have many Autors. In link above it is written that I can use a query for a form field. So I could prepare a query that would select authors and irder them by name. How to manage this? How to get EntityManager there in order to create query and pass it through query option?

protected function configureFormFields(FormMapper $formMapper)
{
    $formMapper
        ->add('name','text')
        ->add('author', 'sonata_type_model', array('query' => ....), array('edit' => 'inline'))
    ;
}

推荐答案

好的,我搞定了:

/**
 * @param \Sonata\AdminBundle\Form\FormMapper $formMapper
 * @return void
 */
protected function configureFormFields(FormMapper $formMapper)
{
    $entity = new \MyCompany\MyProjectBundle\Entity\Seria();
    $query = $this->modelManager->getEntityManager($entity)->createQuery('SELECT s FROM MyCompany\MyProjectBundle\Entity\Seria s ORDER BY s.nameASC');

    $formMapper
        ->add('title', 'text')
        ->add('seria', 'sonata_type_model', array('required' => true, 'query' => $query), array('edit' => 'standard'))
        ->add('description', 'textarea',
               array('attr' => array('class' => 'tinymce'), 'required' => false))
    ;
}

这篇关于SonataAdminBundle 表单字段查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-13 14:39