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


基础教程推荐
- 有什么方法可以用编码 UTF-8 而不是 Unicode 返回 PHP`json_encode`? 2021-01-01
- 如何在 Laravel 中使用 React Router? 2022-01-01
- PHP 类:全局变量作为类中的属性 2021-01-01
- 在PHP中根据W3C规范Unicode 2022-01-01
- PHP PDO MySQL 查询 LIKE ->多个关键词 2021-01-01
- 如何在 Laravel 5.3 注册中添加动态下拉列表列? 2021-01-01
- 学说 dbal querybuilder 作为准备好的语句 2022-01-01
- YouTube API v3 点赞视频,但计数器不增加 2022-01-01
- 如何替换eregi() 2022-01-01
- Cron Jobs 调用带有变量的 PHP 脚本 2022-01-01