【问题标题】:QueryBuilder in the BuildFormBuildForm 中的 QueryBuilder
【发布时间】:2016-08-31 03:07:09
【问题描述】:

我有一个与实体 Employe 具有 OneToMany 关系的实体 Equipe。这意味着一个团队有很多员工。因此,在 EquipeType 中,我尝试在每个人前面显示员工列表和一个复选框,如果我想在该团队中添加一名员工,我只需要检查它。这行得通,但我的问题是如何显示名称、ID 和所有其他属性并将它们放在表中。我需要一个 for 语句,但要在里面放什么?谢谢this is how I get it in my twig

我的表单生成器

->add('date')
        ->add('nom')
        ->add('employes', 'entity', array(
            'class' => 'OCUserBundle:Employe',
            'property' => 'username',
            'multiple' => true,
            'expanded' => true,
            'query_builder' => function (EntityRepository $er) {
                return $er->createQueryBuilder('u')
                    ->orderBy('u.id', 'ASC');
            },

我的树枝

{{form_widget(form.employes)}}

【问题讨论】:

  • 你用的是什么 Symfony2 版本?
  • 我使用的是 Symfony 2.6

标签: forms symfony symfony-2.6


【解决方案1】:

由于 Symfony 2.7 中的 property 选项已更改为 choice_label 并且可能是这样的回调

'choice_label' => function ($employee) {
    return $employee->getName().'/'.$employee->getId();
}

这在EntityType field documentation中有解释

如果您使用早于 2.7 版本的 Symfony,您应该在 Employee 中声明一个方法,如下所示:

public function getDisplayName()
{ 
    return $this->getName().'/'.$this->getId(); 
}

然后在选项中将属性声明为

'property' => 'display_name'

请参阅EntityType field documentation for Symfony 2.6 了解更多信息。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多