我正在使用 Symfony2。我有一个 EMPLOYEE 实体(其中有一个字符串字段“类别”)和一个合同实体。
所以,这是我的问题:
编辑员工后,我可以为他编辑合同。
如果员工属于类别 == '工人',并且如果类别 = 'CEO',我不想显示此字段,我想在我的合同表单中添加字段“薪水”。
这是我的 ContractType :
class ContractType extends AbstractType
{
protected $employee;
function __construct(MyBundle\Entity\Employee $employee = null) {
$this->employee = $employee;
}
public function buildForm(FormBuilder $builder, array $options) {
$builder
->add('startDate');
if ($this->employee !== null && $this->employee->getCategory() == 'worker')
{
$builder
->add('salary', 'money', array('currency' => 'USD', 'required' =>false));
}
elseif ($this->employee !== null && $this->employee->getCategory() == 'CEO')
{
$builder->add('salary', 'hidden', array('required' => false));
}
}
}
这是我的 contract_form.html.twig :
{% if employee.category == 'worker'%}
<tr>
<td>{{ form_label(form.salary, "Salary : ") }}</td>
<td>{{ form_widget(form.salary) }}</td>
</tr>
{% endif %}
After editing a employee and setting him category=='worker', when I want to edit him a contract, I have the error :
Method "salary" for object "Symfony\Component\Form\FormView" does not exist in MyBundle:Contract:contract_form.html.twig"
我被这个错误困住了,我不明白我的代码有什么问题
非常感谢您的帮助!