在 `EntityType` 中使用 `$options` Query_builder
Use `$options` in an `EntityType` Query_builder
我使用表单生成器来创建我的表单。我将选项 team 添加到 FormBuilderInterface
以在我的表单中访问此 属性。
class PersonnalStatType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('tag', EntityType::class, [
'class' => StatTag::class,
'choice_label' => 'name',
'query_builder' => function (StatTagRepository $rep/*, $options*/)
{
return $rep->queryActivated($options['team']);
}
]
)
;
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
'data_class' => PersonnalStat::class,
'team' => null
]);
$resolver->setAllowedTypes('team', ['null', Team::class]);
}
第一行是 EntityType
,我需要使用 team 选项来执行查询,但我不知道如何进行。
到目前为止我试过了:
'query_builder' => function (StatTagRepository $rep)
{
return $rep->queryActivated($options['team']);
}
但查询不知道 $options
Notice: Undefined variable: options
和
'query_builder' => function (StatTagRepository $rep, $options)
{
return $rep->queryActivated($options['team']);
}
但它无法识别 $options
Too few arguments to function App\Form\PersonnalStatType::App\Form{closure}(), 1 passed in /Users/pierrickrambaud/Sites/team-manager/vendor/symfony/doctrine-bridge/Form/Type/EntityType.php on line 32 and exactly 2 expected
有人知道黑客吗?
这应该有效:
'query_builder' => function (StatTagRepository $rep) use ($options) {
return $rep->queryActivated($options['team']);
}
我使用表单生成器来创建我的表单。我将选项 team 添加到 FormBuilderInterface
以在我的表单中访问此 属性。
class PersonnalStatType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('tag', EntityType::class, [
'class' => StatTag::class,
'choice_label' => 'name',
'query_builder' => function (StatTagRepository $rep/*, $options*/)
{
return $rep->queryActivated($options['team']);
}
]
)
;
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
'data_class' => PersonnalStat::class,
'team' => null
]);
$resolver->setAllowedTypes('team', ['null', Team::class]);
}
第一行是 EntityType
,我需要使用 team 选项来执行查询,但我不知道如何进行。
到目前为止我试过了:
'query_builder' => function (StatTagRepository $rep)
{
return $rep->queryActivated($options['team']);
}
但查询不知道 $options
Notice: Undefined variable: options
和
'query_builder' => function (StatTagRepository $rep, $options)
{
return $rep->queryActivated($options['team']);
}
但它无法识别 $options
Too few arguments to function App\Form\PersonnalStatType::App\Form{closure}(), 1 passed in /Users/pierrickrambaud/Sites/team-manager/vendor/symfony/doctrine-bridge/Form/Type/EntityType.php on line 32 and exactly 2 expected
有人知道黑客吗?
这应该有效:
'query_builder' => function (StatTagRepository $rep) use ($options) {
return $rep->queryActivated($options['team']);
}