我有以下代码。
$form = $this->createFormBuilder()
->add('distributor', 'entity', array(
'class' => 'AdminBundle:Customers',
'query_builder' => function(EntityRepository $repository) {
return $repository->createQueryBuilder('c')
->where('c.customerType =:type')
->Andwhere('c.status =:status')
->andWhere('c.district =:district')
->setparameter('status', '1')
->setparameter('type', '1')
->setparameter('district', $this->get("security.context")->getToken()->getUser()->getCustomer()->getDistrict()->getId())
;
},
'property' => 'customerName',
'empty_value' => 'Select Distributor',
'multiple' => FALSE,
'expanded' => FALSE,
'required' => TRUE,
)
)
->add('excel_file', 'file'
->getForm();
当我打算像这样在树枝上使用分配器元素时
{{ form_widget(form.distributor,{ 'attr': {'class': 'input-box'} }) }}
收到错误...... 致命错误:不在对象上下文中时使用$ this
如何在带有表单的symfony QueryBuilder中使用$ this
由于 Rakhitha
答案 0 :(得分:2)
您可能正在使用PHP 5.3,因此您无法在闭包中使用$this
。要解决此问题,请执行以下操作:
$id = $this->get("security.context")->getToken()->getUser()->getCustomer()->getDistrict()->getId();
$form = $this->createFormBuilder()
->add('distributor', 'entity', array(
'class' => 'AdminBundle:Customers',
'query_builder' => function(EntityRepository $repository) use ($id) {
return $repository->createQueryBuilder('c')
->where('c.customerType =:type')
->Andwhere('c.status =:status')
->andWhere('c.district =:district')
->setparameter('status', '1')
->setparameter('type', '1')
->setparameter('district', $id)
;
},
'property' => 'customerName',
'empty_value' => 'Select Distributor',
'multiple' => FALSE,
'expanded' => FALSE,
'required' => TRUE,
)
)
->add('excel_file', 'file'
->getForm();