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

2022-01-16 00:00:00 php symfony doctrine doctrine-orm

在解释实体之间关系的创建时,我发现文档非常糟糕.所以,我不得不向我的 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 ;)

相关文章