How to use a SQL query in configureFormFields ?

1,071 views
Skip to first unread message

Richard

unread,
Oct 24, 2012, 11:06:32 AM10/24/12
to sonata...@googlegroups.com
In my BookAdmin.php file, I wrote this function :

protected function configureFormFields(FormMapper $formMapper) {
$formMapper->add('number', null, array('label' => 'Number'))
->add('title', null, array('label' => 'Title'))
->add('idcategory', 'sonata_type_model',
array('label' => 'Category', 'required' => true),
array('edit' => 'list'));
}

When I create a new book, the generated form displays a jquery popup window to select the category, but it displays all categories. 
I have to display a category sublist (for example, categories where status = active). How can I do that ?

(it's ok if I don't use a 'edit' => 'list' - displays a simple combo box - but the combo box contains too many rows)

Richard

unread,
Oct 25, 2012, 1:51:34 PM10/25/12
to sonata...@googlegroups.com
no idea ? :-(

David

unread,
Oct 25, 2012, 10:17:38 PM10/25/12
to sonata...@googlegroups.com
i'm not sure but you can try to override the createQuery method from your CategoryAdmin
and then (depends of the context) add your criteria to the doctrine queryBuilder (it's a proxy class)

The other way can be use of own adminCRUDController.


Le jeudi 25 octobre 2012 19:51:35 UTC+2, Richard a écrit :
no idea ? :-(

Richard

unread,
Oct 26, 2012, 9:31:06 AM10/26/12
to sonata...@googlegroups.com
Thanks David !

Overriding the createQuery helped me !

;-)

David

unread,
Oct 26, 2012, 10:47:39 AM10/26/12
to sonata...@googlegroups.com
I've just found an eaaaaaaaaaaaaaaaasiest way :

sonata_type_model is like a symfony form choice, just give your choices:

$em = $this->getContainer()->get('doctrine')->getEntityManager();
$choices = $em->getRepository('YourEntity')
    ->findBy(array('your' => 'criteria'));

$formMapper->add('fieldname', 'sonata_type_model', array('choices' => $choices));

That's all

Richard

unread,
Oct 26, 2012, 2:01:46 PM10/26/12
to sonata...@googlegroups.com
OMG !

wonderful !

As easiest as we can. Thanks ;-)
Reply all
Reply to author
Forward
0 new messages