SonataAdminBundle Form query

2,835 views
Skip to first unread message

Tomasz Ignatiuk

unread,
Nov 9, 2011, 3:30:11 PM11/9/11
to sonata...@googlegroups.com

In SonataAdminBundle in Admin class I cannot make an orderBy on ManyToMany field (https://github.com/sonata-project/SonataAdminBundle/issues/328).

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'))
    ;
}

Tomasz Ignatiuk

unread,
Nov 14, 2011, 8:46:58 AM11/14/11
to sonata...@googlegroups.com
Any guess?

I tried:
            ->add('seria', 'sonata_type_model', array('required' => true,  'query' => function(EntityRepository $er) {
                return $er->createQueryBuilder('s')
                    ->orderBy('s.nazwa', 'ASC');
                    $er->getQuery();

but it throws error:

Fatal error: Call to undefined method Closure::execute() in C:\wamp\www\fs\vendor\bundles\Sonata\DoctrineORMAdminBundle\Model\ModelManager.php on line 208

Thomas Rabaix

unread,
Nov 14, 2011, 8:53:02 AM11/14/11
to sonata...@googlegroups.com
The model manager does not accept closure .. so juste provide a query object
--
Thomas Rabaix
http://rabaix.net

Tomasz Ignatiuk

unread,
Nov 14, 2011, 10:51:03 AM11/14/11
to sonata...@googlegroups.com
OK, I got it work:

    /**
     * @param \Sonata\AdminBundle\Form\FormMapper $formMapper
     * @return void
     */
    protected function configureFormFields(FormMapper $formMapper)
    {
        $query = $this->modelManager->getEntityManager()->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))        
        ;
    }

tcowin

unread,
Mar 1, 2012, 1:46:09 PM3/1/12
to sonata...@googlegroups.com
I am able to make this work as well, but only if the field name is one word. If it is more than one part, and gets camelcased, I get a 0 in the field on the create form instead of a select and a plus sign.

These are for fields in the same object, each expressing many-to-one relationships. statusOption and outageTypeOption get 0's, while site and service work correctly.

    /**
     * @var StatusOption
     *
     * @ORM\ManyToOne(targetEntity="StatusOption")
     * @ORM\JoinColumns({
     *   @ORM\JoinColumn(name="status_option_id", referencedColumnName="id")
     * })
     */
    private $statusOption;

    /**
     * @var OutageTypeOption
     *
     * @ORM\ManyToOne(targetEntity="OutageTypeOption")
     * @ORM\JoinColumns({
     *   @ORM\JoinColumn(name="outage_type_option_id", referencedColumnName="id")
     * })
     */
    private $outageTypeOption;

    /**
     * @var SiteOption
     *
     * @ORM\ManyToOne(targetEntity="SiteOption")
     * @ORM\JoinColumns({
     *   @ORM\JoinColumn(name="site_id", referencedColumnName="id", nullable=true)
     * })
     */
    private $site;

    /**
     * @var ServiceOption
     *
     * @ORM\ManyToOne(targetEntity="ServiceOption")
     * @ORM\JoinColumns({
     *   @ORM\JoinColumn(name="service_id", referencedColumnName="id", nullable=true)
     * })
     */
    private $service;

tcowin

unread,
Mar 1, 2012, 5:14:24 PM3/1/12
to sonata...@googlegroups.com
Ahh - never mind - my mistake - this was due to the fact that I did not have Admin set up for StatusOption and OutageTypeOption - I did not want these in Admin, but I did want to be able to custom sort these options...
Reply all
Reply to author
Forward
0 new messages