Бор*_*мов 0 forms variables controller symfony access
我的ListProductsController变量中有$ parentId。我想获取$ parentId值并在我的SearchProductType中使用它:
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('price',EntityType::class,[
'class'=>Product::class,
'choice_label'=>'price',
'choice_value'=>'price',
'placeholder'=>'Default',
'query_builder' => function (EntityRepository $er){
return $er->createQueryBuilder('product')
->innerJoin('product.category','c')
->addSelect('c')
->innerJoin('product.manorwomen','m')
->addSelect('m')
->where('c.parent_id=1')
},
'expanded'=>false,
'multiple'=>false
])
->add('submit',SubmitType::class)
;
}
Run Code Online (Sandbox Code Playgroud)
c.parent_id必须等于控制器中的$ parentId
->where('c.parent_id=$parentId')
Run Code Online (Sandbox Code Playgroud)
怎么做?
将其作为必需(强制性)选项传递到SearchProductType表单
/**
* {@inheritdoc}
*/
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setRequired([
'parentId',
]);
}
Run Code Online (Sandbox Code Playgroud)
然后在创建表单时将其传递 ListProductsController
$form = $this->createForm(SearchProductType::class, $objName,
['parentId' => $parentId], //or whatever the variable is called
);
Run Code Online (Sandbox Code Playgroud)
最终使用它
public function buildForm(FormBuilderInterface $builder, array $options)
{
$parentId = $options['parentId'];
$builder->add('price',EntityType::class,[
[...]
'query_builder' => function (EntityRepository $er) use ($parentId) {
[...]
->where('c.parent_id=' . $parentId)
},
]);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
408 次 |
| 最近记录: |