简体   繁体   English

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

[英]Doctrine2.3 and OneToOne cascade persist doesn't seem to work

I have two entites (User and UserPreferences) that I want to map OneToOne unidirectional. 我有两个entite(User和UserPreferences),我想要单向映射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;

    ...
}

Now when a new User is created, userPreferences is initialized with a new UserPreferences object. 现在,当创建新用户时,将使用新的UserPreferences对象初始化userPreferences。 When trying to persist user , Doctrine throws an Exception, claiming 当试图坚持user ,Doctrine会抛出异常,声称

A new entity was found through the relationship '...\\Entity\\User#userPreferences' that was not configured to cascade persist operations for entity: ...\\Entity\\UserPreferences@000000003ae25e5700000000a6eaafc9. To solve this issue: Either explicitly call EntityManager#persist() on this unknown entity or configure cascade persist this association in the mapping for example @ManyToOne(..,cascade={"persist"}).

But what else should I do? 但我还应该做些什么呢? User#userPreferences is configured to cascade persist but it doesn't. 用户#userPreferences配置为级联持久但不支持。 Am I getting something wrong here? 我在这里弄错了吗?

Ok found the solution: 好的找到了解决方案:

/**
 * 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;

    ...
}

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). 首先,我必须指定mappedBy和inversedBy(我之前已经尝试但是方向错误 - 在拥有方面为mappedBy,在反向方向上反转)。 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. 另外我认为反向的一方不需要有一个单独的id,我试图使用拥有方的id(User#id)作为这个的主键。

Bug was with Bug出现了

 * @ORM\Column(name="user_preferences_id", type="integer")

Your code should contain 您的代码应包含

 * @ORM\JoinColumn(name="user_preferences_id", referencedColumnName="id")

instead of this. 而不是这个。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM