Symfony更新相关实体

时间:2013-11-22 16:56:40

标签: symfony doctrine-orm

假设我们有员工注册系统。我们有四个实体:员工,登录,电话,电子邮件 一名员工拥有许多登录,许多电话和许多电子邮件

我创建了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') ;
}

最后是员工类型,将所有这些联系起来,加上一些额外的字段:

    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 = $this->getDoctrine()->getRepository('AdminBundle:Employee')
            ->find($empId);

然后启动表单并将$ employee与表单联系起来:

$form = $this->createForm(new EmployeeType(), $employee, array(
            'action' => $this->generateUrl('employee_save')
        ));

问题$ employee对象本身在表单字段中正确加载和显示,但所有相关对象都显示为对象(Doctrine \ ORM \ PersistentCollection)[416]并且没有数据被重试。即使我为那些PersistentCollections执行initialize(),数据也会显示在coll属性下,但不会以表单形式显示。

如何正确更新操作?

2 个答案:

答案 0 :(得分:0)

您正在寻找collection field-type EmployeePhone / Email / Login之间的一对多关系

答案 1 :(得分:0)

是收集类型字段在这种情况下是最佳选择,但由于我意识到应用程序实际上需要固定数量的电话和电子邮件字段,然后我删除了单独的实体并将固定数量的条目保存到主实体中。还修复了data_class来纠正一个。

相关问题