<legend id='KiuaG'><style id='KiuaG'><dir id='KiuaG'><q id='KiuaG'></q></dir></style></legend>
  • <small id='KiuaG'></small><noframes id='KiuaG'>

    <tfoot id='KiuaG'></tfoot>
        <bdo id='KiuaG'></bdo><ul id='KiuaG'></ul>

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

        是否可以为 JoinColumn 引用除“id"之外的列?

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

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

              <tbody id='Y82ph'></tbody>
            <tfoot id='Y82ph'></tfoot>

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

              • <legend id='Y82ph'><style id='Y82ph'><dir id='Y82ph'><q id='Y82ph'></q></dir></style></legend>

                  本文介绍了是否可以为 JoinColumn 引用除“id"之外的列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我有一个 Item 实体,它与 Category 实体具有 ManyToOne 关系.我希望它们通过类别的 id 以外的字段(在本例中为名为 id2 的字段)加入.我的架构如下所列.

                  I have an Item entity that has a ManyToOne relationship to a Category entity. I want them to be joined by a field other than Category's id (in this case, a field called id2). My schema is listed below.

                  class Item {
                      /**
                       * @ORMId
                       * @ORMColumn(name = "id", type = "integer")
                       * @ORMGeneratedValue(strategy = "AUTO")
                       */
                      protected $id;
                      /**
                       * @ORMManyToOne(targetEntity = "Category")
                       * @ORMJoinColumn(name = "category_id", referencedColumnName = "id2")
                       */
                      protected $category;
                  }
                  
                  class Category {
                      /**
                       * @ORMId
                       * @ORMColumn(name = "id", type = "integer")
                       * @ORMGeneratedValue(strategy = "AUTO")
                       */
                      protected $id;
                      /**
                       * @ORMColumn(name = "id2", type = "string", length = "255", unique = "true")
                       */
                      protected $id2;
                  

                  当我尝试保存 Item 时,出现此错误:

                  When I try saving an Item I get this error:

                  注意:未定义索引:id2 in vendor/doctrine/lib/Doctrine/ORM/Persisters/BasicEntityPersister.php line 511

                  Notice: Undefined index: id2 in vendor/doctrine/lib/Doctrine/ORM/Persisters/BasicEntityPersister.php line 511

                  果然,如果我在 JoinColumn 注释中将 id2 更改为 id ,一切正常,但我需要连接实体通过 id2.这可能吗?

                  Sure enough, if I change id2 to id in the JoinColumn annotation, everything works fine, but I need the entities to be connected through id2. Is this possible?

                  编辑
                  根据官方Doctrine 2文档,我想要实现的目标是不可能的.

                  Edit
                  What I want to achieve is impossible according to the official Doctrine 2 docs.

                  不能使用指向非主键的连接列.Doctrine 会认为这些是主键并创建延迟加载代理数据,这可能会导致意想不到的结果.教义可以出于性能原因无法验证此的正确性在运行时进行设置,但只能通过 Validate Schema 命令进行设置.

                  It is not possible to use join columns pointing to non-primary keys. Doctrine will think these are the primary keys and create lazy-loading proxies with the data, which can lead to unexpected results. Doctrine can for performance reasons not validate the correctness of this settings at runtime but only through the Validate Schema command.

                  来源:https://www.doctrine-project.org/projects/doctrine-orm/en/2.6/reference/limitations-and-known-issues.html#join-columns-with-non-primary-keys

                  推荐答案

                  我认为 Doctrine 希望这些成为主键,来自 文档:

                  I think Doctrine wants these to be primary keys, from the docs:

                  name:保存此关系的外键标识符的列名.

                  name: Column name that holds the foreign key identifier for this relation.

                  从你的代码示例中跳出来的另一件事是 category.id2type string,我至少希望它是一个整数,但它也可能需要 @JoinColumn 才能正常工作.

                  Another thing that jumps out at me from your code sample is category.id2 being type string, I would at least expect it to be an integer, but it may also need to be for @JoinColumn to work properly.

                  您可能只需要 category.id2 上的 @Index 并将其保留为 string;无论如何值得一试.

                  You may be able to get away with just @Index on category.id2 and leave it as a string though; worth a shot anyway.

                  这篇关于是否可以为 JoinColumn 引用除“id"之外的列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:Doctrine2 findBy 关系对象触发字符串转换错误 下一篇:不调用具有抽象基类的 Doctrine 2 LifecycleCallbacks

                  相关文章

                  • <bdo id='NjWrJ'></bdo><ul id='NjWrJ'></ul>

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

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