问题
Here is my problem.
I use the buildForm
method on symfony 2.1 in order to build my form.
With the following code everything works just fine :
$builder->add('combat','entity',array(
class' => 'KarateCompetitionBundle:CompetitionCombat',
'empty_value' => 'Sélectionner un combat'));
But I want to filter and display only some Combat
. That's why I have to use the query_builder
option. When i do that i get the This value is not valid
error message.
Here is the code :
$builder->add('combat','entity',array(
'class' => 'KarateCompetitionBundle:CompetitionCombat',
'empty_value' => 'Sélectionner un combat',
'query_builder' => function(CombatRepository $cr) {
return $cr->getAllWithoutBilanQueryBuilder();}));
I reduce at the minimum the code (i.e. no filtering on the getAllWithoutBilanQueryBuilder
method) in order to be able to find the problem.
public function getAllWithoutBilanQueryBuilder(){
$queryBuilder = $this->getEntityManager()->createQueryBuilder();
return $queryBuilder->select('c')->from('KarateEntrainementBundle:CompetitionCombat', 'c');
}
I've compared both html code generate on each case and they are the same.
I put a var_dump($object)
on the controller after binding the form with the request $form->bind($request)
and it appears that when i use the query_builder
option the combat
is null while it is not null if i don't use it.
I can't manage to understand why ? I found few post on the web with the same issue but none with an answer. Is it possible that there is a symfony issue here or am I doing something wrong ?
回答1:
I had the exact same problem and - in my case - traced it back to Symfony\Bridge\Doctrine\Form\ChoiceList\ORMQueryBuilderLoader.
When the form is validated, the entities are loaded by primary key in ORMQueryBuilderLoader::getEntitiesByIds() by adding an IN() clause to the query builder. In my case, this IN() clause was ineffective and all selectable entities were returned.
This, in turn, caused Symfony\Component\Form\Extension\Core\DataTransformer\ChoicesToValuesTransformer::reverseTransform() to throw a TransformationFailedException, because the number of loaded entities and submitted choices were not the same.
I suppose there's other possible causes for this specific error. Here's what you could try:
- Look at the generated query, run it manually and make sure it returns only the selected values
- In Symfony\Component\Form\Form, try outputting the caught TransformationFailedException and see where it leads you.
- If none of the above seem plausible, add some debug output to Symfony\Component\Form\Extension\Validator\Constraints\FormValidator and see whether you can narrow it down somewhat.
回答2:
As an addition to @mike-b answer: for me failing QueryBuilder statement was ->having('...') query part:
...
'query_builder' => function(EntityRepository $er) use ($pn) {
return $er->createQueryBuilder('p')
->select('p')
->join('p.product', 'pr')
->where('p.name = ' . $pn->getId())
->andWhere("LENGTH(p.value_en) > 1")
->andWhere("p.value_en != ''")
/* when I remove this - validation passe flawlessly */
->having('COUNT(pr.id) > 1')
->groupBy('p.value_en)
->orderBy('p.value_en', 'ASC');
...
tested with Symfony version 2.3.6
回答3:
I finally managed to make this worked :-)
So here was the fix.
On the getAllWithoutBilanQueryBuilder
function, I replace
$queryBuilder = $this->getEntityManager()->createQueryBuilder();
by
$queryBuilder = $this->createQueryBuilder('c');
I don't know what is exactly the difference and why this is now working. But this is working.
Thanks you all for your help.
来源:https://stackoverflow.com/questions/15192735/this-value-is-not-valid-when-using-query-builder-on-entity-buildform-on-sy