<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 中实现多对多和一对多?

      How to ManyToMany and OneToMany in Symfony and Doctrine?(如何在 Symfony 和 Doctrine 中实现多对多和一对多?)
            <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 中实现多对多和一对多?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                相关文档推荐

                DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以
                PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的
                mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)
                Laravel Gmail Configuration Error(Laravel Gmail 配置错误)
                Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)
                Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)
                • <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'>

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

                      • <tfoot id='fnFZe'></tfoot>