本文介绍了Doctrine2.3和OneToOne级联持续性似乎不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

代码看起来像这样:

我有两个意思(用户和用户首选项),我想映射OneToOne单向。

The code looks something like this:

/**
 * @ORM\Table("users")
 * @ORM\Entity
 */
class User
{
    /**
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     */
    protected $id;

    ...

    /**
     * @ORM\Column(name="user_preferences_id", type="integer")
     * @ORM\OneToOne
     * (
     *      targetEntity="UserPreferences",
     *      cascade={"persist"}
     * )
     */
    protected $userPreferences;

    public function __construct() {
        $this->userPreferences = new UserPreferences();
    }
}


/**
 * @ORM\Table("user_preferences")
 * @ORM\Entity
 */
class UserPreferences extends UserPreferencesEntity
{
    /**
     * @ORM\Id
     * @ORM\Column(name="user_id", type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

    ...
}

现在当一个新用户被创建,userPreferences被初始化一个新的UserPreferences对象。当试图坚持用户时,Doctrine会抛出异常,声称

Now when a new User is created, userPreferences is initialized with a new UserPreferences object. When trying to persist user, Doctrine throws an Exception, claiming

一个新的实体是通过关系... \Entity\User#userPreferences找到未配置为级联实体的持久性操作:... \Entity\UserPreferences @ 000000003ae25e5700000000a6eaafc9。要解决这个问题:在这个未知实体上显式调用EntityManager#persist()或者在映射中配置cascade持久化这个关联,例如@ManyToOne(..,cascade = {persist})。

但我还应该做些什么? User#userPreferences被配置为级联持久但不是。我在这里遇到什么问题吗?

But what else should I do? User#userPreferences is configured to cascade persist but it doesn't. Am I getting something wrong here?

推荐答案

确定解决方案:

/**
 * User
 *
 * @ORM\Table("users")
 * @ORM\Entity
 */
class User extends UserEntity
{
    ...

    /**
     * @ORM\OneToOne
     * (
     *      targetEntity="UserPreferences",
     *      cascade={"persist", "remove"},
     *      inversedBy="user"
     * )
     */
    protected $userPreferences;
}

/**
 * @ORM\Table("user_preferences")
 * @ORM\Entity
 */
class UserPreferences extends UserPreferencesEntity
{
    /**
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue
     */
    protected $id;

    /**
     * @var int
     *
     * @ORM\OneToOne(targetEntity="User", mappedBy="id", cascade={"persist", "remove"})
     */
    protected $user;

    ...
}

首先我有指定mappedBy和inversedBy(我之前已经尝试过,但是在错误的方向 - 在所有方面为mappedBy,inversedBy为反向)。另外我认为反转的一面不需要一个单独的id,我试图使用所有方的id(User#id)作为这个主键。

First of all I had to specify mappedBy and inversedBy (which I already tried before but in the wrong direction - mappedBy at the owning side, inversedBy at inversed side). Also I thought that the inversed side did not need to have a separate id and I tried to use the id of the owning side (User#id) as primary key for this one too.




  • http://docs.doctrine-project.org/en/latest/reference/unitofwork-associations.html
  • http://docs.doctrine-project.org/en/latest/reference/association-mapping.html

这篇关于Doctrine2.3和OneToOne级联持续性似乎不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-06 00:09