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

2024-08-09php开发问题
1

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

The End

相关推荐

PHP实现DeepL翻译API调用
DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以...
2025-08-20 php开发问题
168

PHP通过phpspreadsheet导入Excel日期数据处理方法
PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的...
2024-10-23 php开发问题
287

mediatemple - 无法使用 codeigniter 发送电子邮件
mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)...
2024-08-23 php开发问题
11

Laravel Gmail 配置错误
Laravel Gmail Configuration Error(Laravel Gmail 配置错误)...
2024-08-23 php开发问题
16

将 PHPMailer 用于 SMTP 的问题
Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)...
2024-08-23 php开发问题
4

关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题
Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)...
2024-08-23 php开发问题
17