The form Entity type - symfony

Anyone has any idea how to add some custom options to an entity field type in a nice and clean way? e.g
->add('groups', 'entity', array(
'class' => 'MyBundle:Groups',
'multiple' => 'true',
'query_builder' => function(GroupsRepository $er) {
return $er->createQueryBuilder('g')
->orderBy('g.name', 'ASC');
}))
Let's say that query builder returns "Group1", "Group2" and "Group3". What I want is to "inject" another option called "Any Group" for example.
Thanks,Stelian

The EntityType is a child of the ChoiceType. So, you have access to the same options.
->add('groups', 'entity', array(
'class' => 'MyBundle:Groups',
'multiple' => 'true',
'query_builder' => function(GroupsRepository $er) {
return $er->createQueryBuilder('g')->orderBy('g.name', 'ASC');
},
'empty_value' => 'Any group',
))

Related

Symfony 2, choices from database

I'm trying to display a form with a "select" where is the "options" from a query.
$results = $conn->query(" select gsm, name from Contact");
$row = $results->fetchAll();
and after that: I trying to use this
foreach($row as $lign)
$centretechnique[$lign['gsm']] = $lign['name'];
and in formbuilder
$form->add('centretechnique', 'choice', array('required' => false, 'error_bubbling' => true, "empty_value" => "Choisir ", 'choices' => $centretechnique));
I want to display a list of select option with value=gsm and the displayed value is the name
this method llow me to display , but the problem that if I have duplicate option, only on option will be displayed.
for example, the result of the request is
array (size=2)
0 =>
array (size=2)
'gsm' => string '628436515' (length=9)
'name' => string 'name1' (length=7)
1 =>
array (size=2)
'gsm' => string '628436515' (length=9)
'name' => string 'name 2' (length=4)
just one option will be displayed.
any help please
If you want to populate choices form type from the database, then you should be using the form entity type.
Something like this:
$form->add(
'centretechnique',
'entity',
array(
'class' => 'MyBundle:Contact',
'query_builder' => function (EntityRepository $er) {
return $er->createQueryBuilder('c')
->orderBy('c.name', 'ASC');
},
'choice_label' => 'name',
'choice_value' => 'gsm',
'multiple' => false,
'expanded' => false,
'required' => false,
)
)
By building your choices you do :
$centretechnique[$lign['gsm']] = $lign['name'];
So your code does overwrite the first choice with the second, since you use the same key twice ...

How to sort many2one choices other than by id in Sonata Admin?

Everything is in the title: i can't manage to retrieve data otherwise than sorted by id. I would like them sorted by name, by example. How can I do that ?
$formMapper
->with('Général')
->add('name', 'text', array('label' => 'Nom'))
->add('allergen', 'entity', array(
'label' => 'Allergène',
'class' => 'Proxymart\ProductBundle\Entity\Allergen',
'required' => false))
->end()
;
The result is this (sorted by id, not good):
Any help would be appreciated...
One way would be to pass query_builder option, like this:
'query_builder' => function(\Doctrine\ORM\EntityRepository $repo) {
return $repo->createQueryBuilder('a')
->orderBy('a.name', 'ASC');
},

Symfony 2, Gedmo Tree: dropdown select sorted by title

I'm trying to create a dropdown select menu from my directories. Each directory has subdirectories and I'm using a Gedmo Tree Extension to implement it. I created dropdown in this way:
$form = $this->createFormBuilder()
->add('parentDirectory', 'entity', array(
'required' => false,
'label' => 'Directories',
'class' => 'TestTestBundle:Directory',
'attr' => array('class' => 'hidden nextSelect 2-select'),
'empty_value' => 'Choose directory',
'property' => 'indentedName',
'multiple' => false,
'expanded' => false,
'query_builder' => function(DirectoryRepository $r) {
return $r->createQueryBuilder('c')
->orderBy('c.root', 'ASC')
->addOrderBy('c.lft', 'ASC');
}
It works fine, but I need to order all directories also by its title. Is there any way to do this?
This should work:
->orderBy('c.root, c.lvl, c.title', 'ASC')
'lft' doesn't take into account for 'lvl' and 'title'

symfony form builder update option field

is it possible to update an option field after adding it ?
$builder
->add('examens', 'entity', array(
'class' => 'TelegrammeExamenBundle:ExamExamen',
'property' => 'libelle',
'required' => true,
'empty_value' => 'Sélectionnez un examen',
//'data' => $this->em->getReference("TelegrammeExamenBundle:ExamExamen", 510),
'data' => null,
'query_builder' => function(ExamenRepository $r) {
return $r->getSelectList();
},
'attr' => array('class' => 'bg_white_filet_gris')
))
;
how modify field option ??? (setOption don't exist)
if (...) $builder->get('examens')->setOption('property', 'test');
You can simply ->add() it again. As the API documentation suggests for the add method:
Adds or replaces a child to the form
http://api.symfony.com/2.8/Symfony/Component/Form/FormInterface.html#method_add
This can be used to modify form elements for example in a FormEvent.
Alternatively the FormBuilder provides a setAttribute() method which can be used as follows:
$builder->get('examens')->setAttribute('property', 'test');

Set default value on entity list in Symfony

I defined a form entity list:
->add('businessTime', 'entity', array(
'class' => 'AcmeDemoBundle:BusinessTimes',
'property' => 'name',
'required' => true
);
With this the list is displayed but with blank option as default. I would like to display a selected option from the entity by default.
you can use the data property for that
->add('businessTime', 'entity', array(
'class' => 'AcmeDemoBundle:BusinessTimes',
'property' => 'name',
'data' => 1234456,
'required' => true
);

Resources