Symfony更新相关实体
假设我们有员工登记系统。我们有四个实体:员工、登录、电话、电子邮件 一名员工有许多登录名、密码、许多电话和许多电子邮件 我已创建登录类型:Symfony更新相关实体,symfony,doctrine-orm,Symfony,Doctrine Orm,假设我们有员工登记系统。我们有四个实体:员工、登录、电话、电子邮件 一名员工有许多登录名、密码、许多电话和许多电子邮件 我已创建登录类型: public function buildForm(FormBuilderInterface $builder, array $options) { $builder ->add('username') ->add('password'); } 然后是电话类型: public functi
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') ;
}
最后是员工类型,是什么将所有员工联系在一起,再加上一些额外的字段:
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操作时,我首先在controller中加载employee对象:
$employee = $this->getDoctrine()->getRepository('AdminBundle:Employee')
->find($empId);
然后启动表单并将$employee与表单绑定:
$form = $this->createForm(new EmployeeType(), $employee, array(
'action' => $this->generateUrl('employee_save')
));
问题$employee对象本身在表单字段中被正确加载和显示,但所有相关对象都显示为object(doctor\ORM\PersistentCollection)[416],并且不会检索任何数据。即使我为这些PersistentCollections初始化(),数据也会显示在coll atribute下,但不会显示在表单中
如何正确执行更新操作?您正在查找
员工和电话/电子邮件/登录是收集类型字段在这种情况下是最佳选择,但由于我意识到应用程序实际上需要固定数量的电话和电子邮件字段,所以我删除了单独的实体,并将固定数量的条目保存到主实体中。还将数据类修改为正确的类型。您是否尝试将数据类设置为正确的类型?