E
E
e-hot2015-09-03 18:10:14
symfony
e-hot, 2015-09-03 18:10:14

How to specify in the option value attribute the value of the option itself for the entity field in a form in Symfony 2?

Welcome all. Need a hint how to solve the following misunderstanding:
1. There is a form class ( this form is embedded into another form ), in which there is only one field of the entity type:

...
$builder->add( 'olcode', 'entity', array( 'label' => 'Код: ',
                                                   'class' => 'Acme\AppBundle\Entity\Show\Oldemand',
                                                   'query_builder' => function ( EntityRepository $er ) {
                                                                                    return $er->createQueryBuilder( 'u' )
                                                                                              ->groupBy( 'u.olcode' )
                                                                                              ->orderBy( 'u.olcode', 'ASC' );
                                                                        },
                                                   'property' => 'olcode',
                                                   'required' => false,
                                                )
                    );
...

2. Here, through the entity class from the database table, I pull out unique values ​​​​( string ) from all records into the drop-down list - the list reflects all unique values ​​\u200b\u200bin a single instance - everything is fine, what you need.
3. But I found a strange thing in the html markup:
...
<option value="2137864">Silver</option>
...

should be:
...
<option value="Silver">Silver</option>
...

I was looking for where 2137864 could come from, well, similar numerical values ​​in value, it turned out that Symfony 2 for some reason chains values ​​from the same table from another column, although in the entity 'Acme\AppBundle\Entity\Show\Oldemand' these olcode and, let's call its posrepair_id do not intersect in any way.
Question: if anyone has come across such a situation - tell me what is not so spelled out. Thank you.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Denis, 2015-09-03
@prototype_denis

"Under the hood" is the following (very simplified):

$choiseList = [];
foreach ($entities as $entity) {
    $choiseList[$entity->getId()] = (string) $entity;
}

// $choiseList = [
//     5 => 'A',
//     6 => 'B',
//     81 => 'F',
//     // ...
// ];
// 
// <option value="5">A</option>
// <option value="6">B</option>
// <option value="81">F</option>

And this is normal behavior.
After all, no one knows what you have planted on __toString, because of this, an id is used, by which, after submitting the form, entities are deleted, changed or created in the future. This is logical behavior.
For the same values ​​both there and there, use a listener. Read more in the documentation...
Change before the view, and then after the form is submitted, if needed.
In my opinion, you come up with a problem and try to solve it.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question