"This value is not valid" when using query_builder in the entity buildForm on symfony2.1

Here is my problem. I am using the buildForm method for symfony 2.1 to create my form. Everything works fine with the following code:

 $builder->add('combat','entity',array( class' => 'KarateCompetitionBundle:CompetitionCombat', 'empty_value' => 'Sélectionner un combat')); 

But I want to filter and display only some Combat . Therefore, I have to use the query_builder parameter. When I do this, I get an error message This value is not valid . 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 cut at least the code (i.e. does not filter the getAllWithoutBilanQueryBuilder method) to find the problem.

 public function getAllWithoutBilanQueryBuilder(){ $queryBuilder = $this->getEntityManager()->createQueryBuilder(); return $queryBuilder->select('c')->from('KarateEntrainementBundle:CompetitionCombat', 'c'); 

}

I compared both generations of HTML in 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 turned out that when I use the query_builder parameter, Combat is null until it is null unless I use it .

I can’t understand why? I found several posts on the Internet with the same problem, but with none of them. Is it possible that there is a problem with symfony here or am I doing something wrong?

+3
source share
3 answers

I finally managed to do this :-)

So here is the fix. In the getAllWithoutBilanQueryBuilder function getAllWithoutBilanQueryBuilder I replace

 $queryBuilder = $this->getEntityManager()->createQueryBuilder(); 

 $queryBuilder = $this->createQueryBuilder('c'); 

I don’t know what the difference is and why it is working now. But it does work.

Thank you all for your help.

0
source

I had the same problem and - in my case - traced it to Symfony \ Bridge \ Doctrine \ Form \ ChoiceList \ ORMQueryBuilderLoader.

When the form is validated, the objects are loaded with the primary key in ORMQueryBuilderLoader :: getEntitiesByIds (), adding the IN () clause to the query builder. In my case, this IN () clause was inefficient and all selectable objects were returned.

This, in turn, caused the appearance of Symfony \ Component \ Form \ Extension \ Core \ DataTransformer \ ChoicesToValuesTransformer :: reverseTransform () to exclude TransformationFailedException, because the number of loaded objects and the presented options was not the same.

I suggest that there are other possible causes for this particular error. Here is what you could try:

  • Look at the generated query, run it manually and make sure that it returns only the selected values
  • In Symfony \ Component \ Form \ Form, try to throw the caught TransformationFailedException and see where it will lead you.
  • If none of the above seems plausible, add some debugging output to Symfony \ Component \ Form \ Extension \ Validator \ Constraints \ FormValidator and see if you can narrow it down a bit.
+1
source

As a complement to @ mike-b's answer: for me, an invalid QueryBuilder query was → having ("...") a part of the query:

  ... '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

+1
source

Source: https://habr.com/ru/post/955038/


All Articles