<tfoot id='BfVkD'></tfoot>
      1. <legend id='BfVkD'><style id='BfVkD'><dir id='BfVkD'><q id='BfVkD'></q></dir></style></legend>

        <small id='BfVkD'></small><noframes id='BfVkD'>

        <i id='BfVkD'><tr id='BfVkD'><dt id='BfVkD'><q id='BfVkD'><span id='BfVkD'><b id='BfVkD'><form id='BfVkD'><ins id='BfVkD'></ins><ul id='BfVkD'></ul><sub id='BfVkD'></sub></form><legend id='BfVkD'></legend><bdo id='BfVkD'><pre id='BfVkD'><center id='BfVkD'></center></pre></bdo></b><th id='BfVkD'></th></span></q></dt></tr></i><div id='BfVkD'><tfoot id='BfVkD'></tfoot><dl id='BfVkD'><fieldset id='BfVkD'></fieldset></dl></div>
          <bdo id='BfVkD'></bdo><ul id='BfVkD'></ul>
      2. Doctrine2.3 和 OneToOne 级联持续似乎不起作用

        时间:2023-08-18
        <i id='eeaDo'><tr id='eeaDo'><dt id='eeaDo'><q id='eeaDo'><span id='eeaDo'><b id='eeaDo'><form id='eeaDo'><ins id='eeaDo'></ins><ul id='eeaDo'></ul><sub id='eeaDo'></sub></form><legend id='eeaDo'></legend><bdo id='eeaDo'><pre id='eeaDo'><center id='eeaDo'></center></pre></bdo></b><th id='eeaDo'></th></span></q></dt></tr></i><div id='eeaDo'><tfoot id='eeaDo'></tfoot><dl id='eeaDo'><fieldset id='eeaDo'></fieldset></dl></div>
          • <bdo id='eeaDo'></bdo><ul id='eeaDo'></ul>
            <tfoot id='eeaDo'></tfoot>
              1. <small id='eeaDo'></small><noframes id='eeaDo'>

                  <tbody id='eeaDo'></tbody>
                <legend id='eeaDo'><style id='eeaDo'><dir id='eeaDo'><q id='eeaDo'></q></dir></style></legend>

                1. 本文介绍了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 ORM 不会刷新在脚本之外更改的对象 下一篇:如何判断当前交易是否会改变 Doctrine 2 的任何实体?

                  相关文章

                2. <tfoot id='vABFa'></tfoot>
                  <legend id='vABFa'><style id='vABFa'><dir id='vABFa'><q id='vABFa'></q></dir></style></legend>

                    <small id='vABFa'></small><noframes id='vABFa'>

                    <i id='vABFa'><tr id='vABFa'><dt id='vABFa'><q id='vABFa'><span id='vABFa'><b id='vABFa'><form id='vABFa'><ins id='vABFa'></ins><ul id='vABFa'></ul><sub id='vABFa'></sub></form><legend id='vABFa'></legend><bdo id='vABFa'><pre id='vABFa'><center id='vABFa'></center></pre></bdo></b><th id='vABFa'></th></span></q></dt></tr></i><div id='vABFa'><tfoot id='vABFa'></tfoot><dl id='vABFa'><fieldset id='vABFa'></fieldset></dl></div>
                      <bdo id='vABFa'></bdo><ul id='vABFa'></ul>