沃梦达 / 编程问答 / php问题 / 正文

Doctrine2.3 和 OneToOne 级联持续似乎不起作用

Doctrine2.3 and OneToOne cascade persist doesn#39;t seem to work(Doctrine2.3 和 OneToOne 级联持续似乎不起作用)

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

问题描述

我有两个实体(User 和 UserPreferences),我想单向映射 OneToOne.

I have two entites (User and UserPreferences) that I want to map OneToOne unidirectional.

代码如下所示:

/**
 * @ORMTable("users")
 * @ORMEntity
 */
class User
{
    /**
     * @ORMColumn(name="id", type="integer")
     * @ORMId
     */
    protected $id;

    ...

    /**
     * @ORMColumn(name="user_preferences_id", type="integer")
     * @ORMOneToOne
     * (
     *      targetEntity="UserPreferences",
     *      cascade={"persist"}
     * )
     */
    protected $userPreferences;

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


/**
 * @ORMTable("user_preferences")
 * @ORMEntity
 */
class UserPreferences extends UserPreferencesEntity
{
    /**
     * @ORMId
     * @ORMColumn(name="user_id", type="integer")
     * @ORMGeneratedValue(strategy="AUTO")
     */
    protected $id;

    ...
}

现在,当一个新用户被创建时,userPreferences 被一个新的 UserPreferences 对象初始化.当尝试持久化 user 时,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

通过关系 '...EntityUser#userPreferences' 发现了一个新实体,该实体未配置为对实体进行级联持久操作:...EntityUserPreferences@000000003ae25e5700000000a6eaafc9.解决这个问题:要么在这个未知实体上显式调用 EntityManager#persist() 要么配置级联在映射中持久化这个关联,例如@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
 *
 * @ORMTable("users")
 * @ORMEntity
 */
class User extends UserEntity
{
    ...

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

/**
 * @ORMTable("user_preferences")
 * @ORMEntity
 */
class UserPreferences extends UserPreferencesEntity
{
    /**
     * @ORMColumn(name="id", type="integer")
     * @ORMId
     * @ORMGeneratedValue
     */
    protected $id;

    /**
     * @var int
     *
     * @ORMOneToOne(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 级联持续似乎不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:Doctrine2.3 和 OneToOne 级联持续似乎不起作用

基础教程推荐