“此值无效”;使用“ query_builder”时在“实体”上在symfony2.1上的buildForm [英] "This value is not valid" when using "query_builder" on "entity" buildForm on symfony2.1

查看:60
本文介绍了“此值无效”;使用“ query_builder”时在“实体”上在symfony2.1上的buildForm的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的问题。
我在symfony 2.1上使用 buildForm 方法来构建表单。
使用以下代码,一切正常:

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'));

但是我只想过滤和显示一些 Combat 。这就是为什么我必须使用 query_builder 选项的原因。当我这样做时,我得到此值无效错误消息。
这是代码:

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();}));

我最少减少代码(即在 getAllWithoutBilanQueryBuilder <上不进行过滤/ code>方法)以发现问题。

I reduce at the minimum the code (i.e. no filtering on the getAllWithoutBilanQueryBuildermethod) in order to be able to find the problem.

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

}

每种情况下生成的html代码都是相同的。

I've compared both html code generate on each case and they are the same.

我在 var_dump($ object)上放了一个控制器将表单与请求 $ form-> bind($ request)绑定后,当我使用 query_builder 选项战斗为空,如果我不使用它,则不为空。

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 combatis null while it is not null if i don't use it.

I无法理解为什么?
我在网上发现很少有相同问题的帖子,但没有答案。
这里可能有symfony问题,还是我做错了事?

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 ?

推荐答案

我终于设法使它起作用:-)

I finally managed to make this worked :-)

这就是解决方法。
getAllWithoutBilanQueryBuilder 函数上,我替换

So here was the fix. On the getAllWithoutBilanQueryBuilder function, I replace

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

通过

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

我不知道到底有什么区别,为什么现在可以奏效。
但这有效。

I don't know what is exactly the difference and why this is now working. But this is working.

感谢大家的帮助。

这篇关于“此值无效”;使用“ query_builder”时在“实体”上在symfony2.1上的buildForm的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆