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

      <tfoot id='KUjWR'></tfoot>

        <legend id='KUjWR'><style id='KUjWR'><dir id='KUjWR'><q id='KUjWR'></q></dir></style></legend>
          <bdo id='KUjWR'></bdo><ul id='KUjWR'></ul>

        学说一对一单向

        时间:2023-07-14
          <tbody id='GBvYN'></tbody>
        <tfoot id='GBvYN'></tfoot>
        1. <i id='GBvYN'><tr id='GBvYN'><dt id='GBvYN'><q id='GBvYN'><span id='GBvYN'><b id='GBvYN'><form id='GBvYN'><ins id='GBvYN'></ins><ul id='GBvYN'></ul><sub id='GBvYN'></sub></form><legend id='GBvYN'></legend><bdo id='GBvYN'><pre id='GBvYN'><center id='GBvYN'></center></pre></bdo></b><th id='GBvYN'></th></span></q></dt></tr></i><div id='GBvYN'><tfoot id='GBvYN'></tfoot><dl id='GBvYN'><fieldset id='GBvYN'></fieldset></dl></div>

              <bdo id='GBvYN'></bdo><ul id='GBvYN'></ul>

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

              • <legend id='GBvYN'><style id='GBvYN'><dir id='GBvYN'><q id='GBvYN'></q></dir></style></legend>
                  本文介绍了学说一对一单向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  在带有 Doctrine 的 Symfony 3 中,我试图与共享相同主键的两个表建立一对一的单向关系.为此,我试图复制 教义关联映射页面.

                  In Symfony 3 with Doctrine I'm trying to get a one-to-one unidirectional relationship with both tables sharing the same primary key working. To do so I'm trying to replicate the example on the Doctrine Association Mapping page.

                  然而,一对一的 uni 文档没有 setter 和 getter 的例子——目标实体上也没有 id 字段的定义.所以我试着围绕自己做实验.

                  However, the one-to-one uni documentation has no examples of setters and getters - and there is no definition of the id field on the target entity, either. So I tried to experiment around myself.

                  这些是我的实体:

                  class Country
                  {
                  
                      /**
                       * @var integer
                       *
                       * @ORMColumn(name="id", type="integer", precision=0, scale=0, nullable=false, unique=false)
                       * @ORMId
                       * @ORMGeneratedValue(strategy="IDENTITY")
                       */
                      private $id;
                  
                      /**
                       * @ORMOneToOne(targetEntity="MySubEntity", cascade={"persist", "remove"})
                       * @ORMJoinColumn(name="id", referencedColumnName="id", nullable=true)
                       */
                      private $mysubentity;
                      [...]
                      /**
                       * @return MySubEntity
                       */
                      public function getMySubEntity()
                      {
                          return $this->mysubentity;
                      }
                  
                      /**
                       * @param MySubEntity $mysubentity
                       */
                      public function setMySubEntity($mysubentity)
                      {
                          $this->mysubentity = $mysubentity;
                      }
                  }
                  
                  class MySubEntity
                  {
                      /**
                       * @var integer
                       *
                       * @ORMColumn(name="id", type="integer", precision=0, scale=0, nullable=false, unique=false)
                       * @ORMId
                       * @ORMGeneratedValue(strategy="IDENTITY")
                       */
                      private $id;
                  
                      [..]
                  
                      /**
                       * Set id
                       *
                       * @param $id
                       *
                       * @return MySubEntity
                       */
                      public function setId($id)
                      {
                          $this->id = $id;
                  
                          return $this;
                      }
                  
                      /**
                       * Get id
                       *
                       * @return int
                       */
                      public function getId()
                      {
                          return $this->id;
                      }
                  }
                  

                  当我保留国家实体时,我收到违反完整性约束:1452 无法添加或更新子行:外键约束失败.在检查数据时,我可以看到 Doctrine 试图将 MySubEntity 的 id 设置为 0.

                  When I persist the country entity, I receive Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails. When inspecting the data I can see that Doctrine attempted to set the id of MySubEntity to 0.

                  有人知道我需要做什么才能从 Country 实体自动填充 MySubEntity $id 字段吗?

                  Does anybody have an idea what I need to do for the MySubEntity $id field to be auto-populated from the Country entity?

                  推荐答案

                  您需要将 JoinColumn name 属性更改为除 id 以外的任何其他内容:

                  You need to change the JoinColumn name property to anything else but not id:

                  /**
                   * @ORMOneToOne(targetEntity="MySubEntity", cascade={"persist", "remove"})
                   * @ORMJoinColumn(name="mysubentity_id", referencedColumnName="id", nullable=true)
                   */
                  private $mysubentity;
                  

                  这是做什么的:JoinColumn 告诉学说该关系保存在哪个数据库列中.因此,如果您将其称为 mysub_id,则您的主实体将有一个具有该名称的列,其中将保留 referencedColumn 值(您的子实体的 ID).

                  What does this do: JoinColumn tells doctrine in which database column the relation is saved. so if you call it mysub_id your main entity will have a column with that name in which the referencedColumn value will be persisted (id of your subEntity).

                  如果您说 JoinColumn 名称是 id,您的实体的主键已经使用了该名称,那么您就会发生冲突.

                  If you say the JoinColumn name is id which is already used by the primary key of your entity you have a conflict.

                  我错过了共享相同主键的观点.这有什么具体原因吗?但是如果你真的因为遗留原因需要这样做,要么看看

                  I missed your point with sharing the same primary key. Is there any specific reason for this? But if you really need to do it for legacy reasons either look at

                  http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/tutorials/composite-primary-keys.html#use-case-2-simple-derived-身份

                  或者可以通过更改生成策略(在这种情况下为 NONE 或自定义)自行生成子实体的主键值

                  or the possibility to generate the primary key value of your subEntity by yourself by changing the generation strategy (NONE or custom in this case)

                  http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/basic-mapping.html#identifier-generation-strategies

                  这篇关于学说一对一单向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:是否可以使用反射修改对象实例的方法 下一篇:Doctrine:扩展实体类

                  相关文章

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

                • <small id='tYDJZ'></small><noframes id='tYDJZ'>

                      <bdo id='tYDJZ'></bdo><ul id='tYDJZ'></ul>