可以说我们有员工注册系统。我们有四个实体:员工,登录,电话,电子邮件 一个员工有许多登录,S,许多手机和ManyEmailsSymfony更新相关实体
我已经创建LoginType:
public function buildForm(FormBuilderInterface $builder, array $options) {
$builder
->add('username')
->add('password');
}
然后手机类型:
public function buildForm(FormBuilderInterface $builder, array $options){
$builder
->add('phone');
}
电子邮件类型
public function buildForm(FormBuilderInterface $builder, array $options){
$builder
->add('eMail') ;
}
最后学历要求,有什么关系所有这些togheder的,再加上一些额外的字段:
public function buildForm(FormBuilderInterface $builder, array $options) {
$builder
->add('firstname')
->add('middleNames')
->add('lastname')
->add('dateofbirth', 'date', array(
'widget' => 'single_text',
'read_only' => true
))
->add('address')
->add('idcode')
->add('position')
->add('phone', new EmployeePhoneType(), array('data_class' => null))
->add('email', new EmployeeEmailType(), array('data_class' => null))
->add('login', new LoginType(), array('data_class' => null))
->add('save', 'submit');
}
在ORM的所有实体有关系
class Employee {
/* ...Other fileds not shown here...*/
/**
* @var Doctrine\Common\Collections\ArrayCollection $login
* @ORM\OneToMany(targetEntity="Crm\AuthBundle\Entity\Login", mappedBy="employee", cascade={"persist"})
*/
protected $login;
/**
* @var Doctrine\Common\Collections\ArrayCollection $phone
* @ORM\OneToMany(targetEntity="Crm\AdminBundle\Entity\EmployeePhone", mappedBy="employee", cascade={"persist"})
*/
protected $phone;
/**
* @var Doctrine\Common\Collections\ArrayCollection $email
* @ORM\OneToMany(targetEntity="Crm\AdminBundle\Entity\EmployeeEmail", mappedBy="employee", cascade={"persist"})
*/
protected $email;
}
class EmployeePhone{
/**
* @ORM\ManyToOne(targetEntity="Crm\AdminBundle\Entity\Employee", inversedBy="phone", cascade={"persist"})
* @ORM\JoinColumn(name="employee_id", referencedColumnName="id", nullable=false)
*/
private $employee;
}
class EmployeeEmail{
/**
* @ORM\ManyToOne(targetEntity="Crm\AdminBundle\Entity\Employee", inversedBy="email", cascade={"persist"})
* @ORM\JoinColumn(name="employee_id", referencedColumnName="id", nullable=false)
*/
private $employee;
}
class Login{
/**
* @ORM\ManyToOne(targetEntity="Crm\AdminBundle\Entity\Employee", inversedBy="login", cascade={"persist"})
* @ORM\JoinColumn(name="employee_id", referencedColumnName="id", nullable=false)
*/
protected $employee;
}
现在,当我做updtade动作我在控制器首先加载Employee对象:
$employee = $this->getDoctrine()->getRepository('AdminBundle:Employee')
->find($empId);
然后发起表格并绑定$员工并填写表格:
$form = $this->createForm(new EmployeeType(), $employee, array(
'action' => $this->generateUrl('employee_save')
));
问题$雇员对象本身被正确加载并在表单字段中显示,但所有相关对象都显示为对象(Doctrine \ ORM \ PersistentCollection)[416],并且没有数据被回收。即使我为这些PersistentCollections初始化(),然后数据显示在coll atribute下,但不显示在窗体中。
如何正确更新操作?
您是否尝试将数据类型设置为正确的类型? –