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

    <legend id='iqh3k'><style id='iqh3k'><dir id='iqh3k'><q id='iqh3k'></q></dir></style></legend>
  1. <small id='iqh3k'></small><noframes id='iqh3k'>

    • <bdo id='iqh3k'></bdo><ul id='iqh3k'></ul>
    <tfoot id='iqh3k'></tfoot>

      如何在 Symfony 和 Doctrine 中实现多对多和一对多?

      时间:2024-08-09
            <i id='Daqho'><tr id='Daqho'><dt id='Daqho'><q id='Daqho'><span id='Daqho'><b id='Daqho'><form id='Daqho'><ins id='Daqho'></ins><ul id='Daqho'></ul><sub id='Daqho'></sub></form><legend id='Daqho'></legend><bdo id='Daqho'><pre id='Daqho'><center id='Daqho'></center></pre></bdo></b><th id='Daqho'></th></span></q></dt></tr></i><div id='Daqho'><tfoot id='Daqho'></tfoot><dl id='Daqho'><fieldset id='Daqho'></fieldset></dl></div>
              <tbody id='Daqho'></tbody>

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

              <bdo id='Daqho'></bdo><ul id='Daqho'></ul>
            • <tfoot id='Daqho'></tfoot>

                <legend id='Daqho'><style id='Daqho'><dir id='Daqho'><q id='Daqho'></q></dir></style></legend>
                本文介绍了如何在 Symfony 和 Doctrine 中实现多对多和一对多?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                在解释实体之间关系的创建时,我发现文档非常糟糕.所以,我不得不向我的 StackExchangers 同伴寻求帮助.所以,我正在尝试构建以下案例:

                I find the documentation very poor when it comes to explaining the creation of relationships between entities. So, i'll have to ask for help to my fellow StackExchangers. So, i'm trying to build the following cases:

                案例 1

                一个User属于一个或多个Group,一个Group可以有多个Permission.User 也可以有一个Permission.

                A User belongs to one or more Group, and a Group can have many Permission. A User also can have a Permission.

                案例 2

                一个Ticket有一个Category、多个Tag和多个Comment.

                A Ticket has a Category, multiple Tag and multiple Comment.

                提前致谢!

                推荐答案

                没问题.首先要了解的是,没有一种方法"可以做到这一点.Doctrine 在您如何 定义关系 - 即使多个定义产生完全相同的 DDL(理解这一点很重要 - 一些映射选择只影响 ORM 的对象端,而不影响模型端)

                Sure thing. First thing to understand is that there is no "one way" to do this. Doctrine gives a lot of flexibility in terms of how you define the relationship - even if multiple definitions produce the exact same DDL (and this is important to understand - some of the mapping choices only effect the object-side of the ORM, not the model-side)

                这是您的用户/组/权限示例,它们实际上都是多对多关联(我排除了所有不相关但必需的代码,例如 PK 列定义)

                Here's your Users/Groups/Permissions example, which are actually all many-to-many associations (I excluded all non-relevant but required code, like PK column definition)

                <?php
                
                namespace YourBundleEntity;
                
                use DoctrineORMMapping as ORM;
                use DoctrineCommonCollectionsArrayCollection;
                
                /**
                 * @ORMEntity
                 */
                class User
                {
                  /**
                   * Many-To-Many, Unidirectional
                   *
                   * @var ArrayCollection $groups
                   *
                   * @ORMManyToMany(targetEntity="Group")
                   * @ORMJoinTable(name="user_has_group",
                   *      joinColumns={@ORMJoinColumn(name="user_id", referencedColumnName="id")},
                   *      inverseJoinColumns={@ORMJoinColumn(name="group_id", referencedColumnName="id")}
                   * )
                   */
                  protected $groups;
                
                  /**
                   * Many-To-Many, Unidirectional
                   *
                   * @var ArrayCollection $permissions
                   *
                   * @ORMManyToMany(targetEntity="Permission")
                   * @ORMJoinTable(name="user_has_permission",
                   *      joinColumns={@ORMJoinColumn(name="user_id", referencedColumnName="id")},
                   *      inverseJoinColumns={@ORMJoinColumn(name="permission_id", referencedColumnName="id")}
                   * )
                   */
                  protected $permissions;
                
                  public function __construct()
                  {
                    $this->groups = new ArrayCollection();
                    $this->permissions = new ArrayCollection();
                  }
                }
                
                /**
                 * @ORMEntity
                 */
                class Group
                {
                  /**
                   * Many-To-Many, Unidirectional
                   *
                   * @var ArrayCollection $permissions
                   *
                   * @ORMManyToMany(targetEntity="Permission")
                   * @ORMJoinTable(name="group_has_permission",
                   *      joinColumns={@ORMJoinColumn(name="group_id", referencedColumnName="id")},
                   *      inverseJoinColumns={@ORMJoinColumn(name="permission_id", referencedColumnName="id")}
                   * )
                   */
                  protected $permissions;
                
                  public function __construct()
                  {
                    $this->permissions = new ArrayCollection();
                  }
                }
                
                /**
                 * @ORMEntity
                 */
                class Permission {}
                

                如果您对这里发生的事情有任何疑问,请告诉我.

                If you have questions about what's going on here, let me know.

                现在,到你的第二个例子

                Now, to your second example

                <?php
                
                namespace YourBundleEntity;
                
                use DoctrineORMMapping as ORM;
                use DoctrineCommonCollectionsArrayCollection;
                
                /**
                 * @ORMEntity
                 */
                class Ticket
                {
                  /**
                   * Many-To-One, Unidirectional
                   *
                   * @var Category
                   *
                   * @ORMManyToOne(targetEntity="Category")
                   * @ORMJoinColumn(name="category_id", referencedColumnName="id")
                   */
                  protected $category;
                
                  /**
                   * Many-To-Many, Unidirectional
                   *
                   * @var ArrayCollection $permissions
                   *
                   * @ORMManyToMany(targetEntity="Tag")
                   * @ORMJoinTable(name="tickt_has_tag",
                   *      joinColumns={@ORMJoinColumn(name="ticket_id", referencedColumnName="id")},
                   *      inverseJoinColumns={@ORMJoinColumn(name="tag_id", referencedColumnName="id")}
                   * )
                   */
                  protected $tags;
                
                  /**
                   * One-To-Many, Bidirectional
                   *
                   * @var ArrayCollection $comments
                   *
                   * @ORMOneToMany(targetEntity="Comment", mappedBy="ticket")
                   */
                  protected $comments;
                
                  public function __construct()
                  {
                    $this->tags = new ArrayCollection();
                    $this->comments = new ArrayCollection();
                  }
                }
                
                /**
                 * @ORMEntity
                 */
                class Comment
                {
                  /**
                   * Many-To-One, Bidirectional
                   *
                   * @var Ticket $ticket
                   *
                   * @ORMManyToOne(targetEntity="Ticket")
                   * @ORMJoinColumn(name="ticket_id", referencedColumnName="id")
                   */
                  protected $ticket=null;
                }
                
                /**
                 * @ORMEntity
                 */
                class Tag {}
                
                /**
                 * @ORMEntity
                 */
                class Category {}
                

                和以前一样,如果您想解释这些,请告诉我.

                As before, let me know if you want any of this explained.

                附:这些都没有经过实际测试,我只是在我的 IDE 中快速完成了它.可能有一两个错字;)

                P.S. None of this was actually tested, I just kinda banged it out in my IDE real fast. There might be a typo or two ;)

                这篇关于如何在 Symfony 和 Doctrine 中实现多对多和一对多?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:在 Doctrine PHP Symfony 中覆盖 Doctrine_Record (sfDoctrineRecord 下一篇:在 ZF2 中创建具有依赖关系(依赖注入)的学说存储库

                相关文章

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

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

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

                  2. <tfoot id='fnFZe'></tfoot>