学说2:如何更新一对一双向

时间:2015-08-03 10:08:28

标签: php doctrine-orm associations

我在更新一对一双向关联时遇到问题。

用户实体

/**
 *
 * @ORM\Table(name="test_user")
 */
class User
{
    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="IDENTITY")
     */
    private $id;

    /**
     * @var string
     *
     * @ORM\Column(name="login", type="string", length=32, nullable=false, unique=true)
     */
    private $login;


    /**
     *
     * @ORM\OneToOne(targetEntity="Points", mappedBy="user", cascade={"persist"})
     */
    private $points;

     ...


    /**
     * Set points
     */
     public function setPoints(array $points)
     {
        $this->points = new Points($points);
        $this->points->setUser($this);

        return $this;
     }

    /**
     * Get points
     */
    public function getPoints()
    {
        return $this->points;
    }

}

点实体

/**
 * Points
 *
 * @ORM\Table(name="test_user_points")
 * @ORM\Entity
 */
class Points {

    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer", nullable=false)
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="IDENTITY")
     */
    private $id;

    /**
     * @var integer
     *
     * @ORM\Column(type="integer",  nullable=false)
     */
    private $points;


    /**
     * @var string
     *
     * @ORM\Column(name="period", type="string", length=24)
     */
     private $period;


    /**
     * @var User
     *
     * @ORM\OneToOne(targetEntity="User", inversedBy="points")
     * @ORM\JoinColumn(name="user_id", referencedColumnName="id",onDelete="CASCADE", nullable=false)
     */
    private $user;





    /**
     * Constructor
     */
    public function __construct(array $params = array())
    {
        $hydrator = new MyHydrator();
        $hydrator->hydrate($params, $this);
    }


    ...

    /**
     * Set user
     *
     * @param User $user
     */
    public function setUser(User $user = null)
    {
        $this->user = $user;

        return $this;
    }

    /**
     * Get user
     *
     * @return User
     */
    public function getUser()
    {
        return $this->user;
    }


}

MyHydrator 正在从数组(第一个参数)转换为对象(第二个参数)。这非常重要,我必须使用它。

我的保存功能如下所示:

public function save(array $data)
{
    ...

    // This is how my input data looks

    $data = array(
        'login' => 'Test',
        array(
            'points' => 999,
            'period' => 'monthly'
        )
    );

    if ($userExists) {
        // UPDATE
        $hydrator = new MyHydrator();
        $hydrator->hydrate($data, $userExists);
        $this->em->persist($userExists);
        $this->em->flush();

    } else {
        // INSERT
        $user = new User($data);
        $this->em->persist($user);
        $this->em->flush();
    }
}

插入数据库工作完美,但当我尝试更新记录时,我收到错误:

SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '4' for key 'UNIQ_DAD93D6EA76ED395'

4 积分

user_id 列的值

如何编辑现有记录而不会出现有关重复ID的错误?

1 个答案:

答案 0 :(得分:0)

它已经过时但是我遇到了类似的问题,我这样解决了: 在用户

public function setPoints($points)
{
    if ($this->points !== null) {
        $this->points->setUser(null);
    }
    $this->points = $points;
    $points->setUser($this);
}