6

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'))
    ;
}
j0k
  • 22,600
  • 28
  • 79
  • 90
Tom
  • 1,203
  • 3
  • 15
  • 35

2 Answers2

9

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))        
    ;
}
Azam Alvi
  • 6,918
  • 8
  • 62
  • 89
Tom
  • 1,203
  • 3
  • 15
  • 35
  • 5
    Take care, with recent version of Sonata, you need to set argument to getEntityManager(), like that: $this->modelManager->getEntityManager('MyCompany\MyProjectBundle\Entity\Seria')->createQuery('SELECT s FROM MyCompanyMyProjectBundle:Seria s ORDER BY s.name ASC'); – bux Jul 04 '12 at 13:42
  • 2
    It works with sf2.3 ? I obtained this error: Class does not exist" at /home/marc/work/play4fun/vendor/doctrine/common/lib/Doctrine/Common/Persistence/AbstractManagerRegistry.php line 199 – Marc Morales Valldepérez Jun 05 '13 at 11:32
  • Thank you for `attr`. Where is the documentation about available formMapper field options ? – Pierre de LESPINAY Jul 16 '13 at 10:15
2

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.

Arnaud Janssens
  • 150
  • 1
  • 12
  • 8
    Works for me like this in Symfony 2.2 `$query_user = $this->modelManager->getEntityManager('Application\Sonata\UserBundle\Entity\User')->createQueryBuilder() ->add('select', 'u') ->add('from', 'Application\Sonata\UserBundle\Entity\User u') ->add('orderBy', 'u.username ASC'); $formMapper ->add('title',null,array('required' => true)) ->add('user', null, array('required' => true, 'query_builder' => $query_user)) ;` – Tom Mar 20 '13 at 20:45
  • Thank you, your code provided me the solution that it was not the modelManager or EntityManager who was causing this bug but inside entity. :) – Arnaud Janssens Mar 22 '13 at 13:00