Request form fields SonataAdminBundle

In SonataAdminBundle in the Admin class, I cannot do orderBy in the ManyToMany field .

For example, the author and the book. An author can have many books, and also a book can have many authors. The link above says that I can use a query for a form field. Therefore, I could prepare a request that would select the authors and burn them by name. How to manage this? How to get EntityManager there to create a request and pass it through the request parameter?

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

OK, I got his job:

/**
 * @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))        
    ;
}
+9
source

- ? " (500 )", .

: Symfony 2.1, Symfony 2.2.

+2

All Articles