Symfony 3 - OneToOne关系更新

时间:2016-11-02 12:56:21

标签: php symfony

我有一个OneToOne关系:

公司实体:

/**
 * @ORM\OneToOne(targetEntity="AppBundle\Entity\Address", mappedBy="company",cascade={"all"})
 */
private $address;

地址实体:

/**
 * @ORM\OneToOne(targetEntity="AppBundle\Entity\Company", inversedBy="address")
 * @ORM\JoinColumn(name="companii_id_companii", referencedColumnName="id_companii")
 */
private $company;

当我尝试更新值时,它会插入一个新地址,而不是更新现有地址。

我做错了什么?

更新

CompanyForm类型:

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder->add("companie_uid", HiddenType::class);
    $builder->add("companie_denumire", TextType::class, array('label' => 'companie_denumire'));
    $builder->add("companie_cui", TextType::class, array('label' => 'companie_cui', 'required' => false));
    $builder->add("companie_j", TextType::class, array('label' => 'companie_j', 'required' => false));
    $builder->add("companie_mail", EmailType::class, array('label' => 'companie_mail', 'required' => false));
    $builder->add("companie_website", TextType::class, array('label' => 'companie_website', 'required' => false));
    $builder->add("companie_status", HiddenType::class);
    $builder->add("companie_descriere", TextAreaType::class, array('label' => 'companie_descriere', 'required' => false));
    $builder->add("companie_telefon", TextType::class, array('label' => 'companie_telefon', 'required' => false));
    $builder->add("companie_iban", TextType::class, array('label' => 'companie_iban', 'required' => false));
    $builder->add("companie_banca", TextType::class, array('label' => 'companie_banca', 'required' => false));
    $builder->add("file", FileType::class, array('label' => 'companie_file', 'mapped' => false, 'required' => false));
    $builder->add("save", SubmitType::class, array('label' => 'companie_save'));
    $builder->add(
        $builder->create('address', CompanyAddressType::class, Array('by_reference' => false,))
    );
}

AddressForm类型

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder->add("adresa_adresa", TextType::class, array('label' => 'adresa_adresa', 'required' => false));
    $builder->add("adresa_telefon", TextType::class, array('label' => 'adresa_telefon', 'required' => false));
    $builder->add("adresa_fax", TextType::class, array('label' => 'adresa_fax', 'required' => false));
    $builder->add("adresa_email", EmailType::class, array('label' => 'adresa_email', 'required' => false));
}

控制器编辑代码:

if ($form->isSubmitted() && $form->isValid()) {
            $company = $form->getData();
            $em->flush();

解决!我只需要删除:

Array('by_reference' => false,)

0 个答案:

没有答案