Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SonataAdminBundle form field query

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

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'))
    ;
}
like image 638
Tom Avatar asked Nov 07 '11 13:11

Tom


2 Answers

OK, I got it work:

/**
 * @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))        
    ;
}
like image 83
Tom Avatar answered Nov 15 '22 08:11

Tom


Anything changed about that ? i'm getting a "class does not exist (500 error)" by using this.

Note : it was working back in Symfony 2.1 but not anymore in Symfony 2.2.

like image 30
Arnaud Janssens Avatar answered Nov 15 '22 09:11

Arnaud Janssens