Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/xamarin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Symfony Doctrine2在保存主实体时重新初始化相关实体(一对一)_Symfony_Doctrine Orm - Fatal编程技术网

Symfony Doctrine2在保存主实体时重新初始化相关实体(一对一)

Symfony Doctrine2在保存主实体时重新初始化相关实体(一对一),symfony,doctrine-orm,Symfony,Doctrine Orm,上下文是一个使用Doctrine2的Symfony2项目(2.0.23) 我有一个具有一对一关系的候选实体网站。创建新候选人时,我将网站实体设置如下: <?php /** * @var Website $website * * @ORM\OneToOne(targetEntity="Website") * @ORM\JoinColumn(name="Website", referencedColumnName="Code") */ private $website; Cand

上下文是一个使用Doctrine2的Symfony2项目(2.0.23)

我有一个具有一对一关系的候选实体
网站
。创建新候选人时,我将
网站
实体设置如下:

<?php

/**
 * @var Website $website
 *
 * @ORM\OneToOne(targetEntity="Website")
 * @ORM\JoinColumn(name="Website", referencedColumnName="Code")
 */
private $website;
Candidate.php

<?php

use MyProject\Bundle\CoreBundle\Entity\Website;

public function initialize(Website $website)
{
    $this->setWebsite($website);
我坚持使用FOSUserBundle的方法:

<?php

/**
 * Updates a user.
 *
 * @param UserInterface $user
 * @param Boolean       $andFlush Whether to flush the changes (default true)
 */
public function updateUser(UserInterface $user, $andFlush = true)
{
    $this->updateCanonicalFields($user);
    $this->updatePassword($user);

    $this->em->persist($user);
    if ($andFlush) {
        $this->em->flush();
    }
}

看来它与有2个
@ORM\Id
注释的主要实体有关。由于它是一个旧的数据库,它不是“学说”友好。在一个自动递增字段中有一个PK,但联接是在第二列上进行的。因此,我删除了PK字段的
@ORM\Id
注释。

您确定在刷新之前始终保留$website对象吗?如何保留您的实体?@Hast:网站不必更新,它只是附加到
候选
实体。
<?php

/**
 * Updates a user.
 *
 * @param UserInterface $user
 * @param Boolean       $andFlush Whether to flush the changes (default true)
 */
public function updateUser(UserInterface $user, $andFlush = true)
{
    $this->updateCanonicalFields($user);
    $this->updatePassword($user);

    $this->em->persist($user);
    if ($andFlush) {
        $this->em->flush();
    }
}