内部联盟如何使用Doctrine和Symfony2在多对多关系中工作 [英] How does inner join work on a many-to-many relationship using Doctrine and Symfony2
问题描述
我最近提出了一个查询 ManyToMany
关系连接表的问题,解决方案与此答案,并且想知道它是如何工作的。
可以说我有一个简单的 ManyToMany
关系组
和团队
,将会在这里自动创建一个 groups_team
表
I recently worked out an issue with querying ManyToMany
relationship join tables, the solution was same as this answer and was wondering how it works.
lets say i have a simple ManyToMany
relationship between groups
and team
, there will be a groups_team
tables that will automatically be created here
groups entity
groups entity
/**
* Groups
*
* @ORM\Table(name="groups")
* @ORM\Entity(repositoryClass="AppBundle\Model\Repository\GroupsRepository")
*/
class Groups {
/**
* @ORM\ManyToMany(targetEntity="Team", inversedBy="group")
*/
protected $team;
public function __construct() {
$this->team = new ArrayCollection();
}
/**
* @var int
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var string
*
* @ORM\Column(name="groupname", type="string", length=255)
*/
private $groupname;
//obligatory getters and setters :)
团队实体
/**
* Team
*
* @ORM\Table(name="team")
* @ORM\Entity(repositoryClass="AppBundle\Model\Repository\TeamRepository")
*/
class Team {
/**
* @ORM\ManyToMany(targetEntity="Groups", mappedBy="team")
*/
protected $group;
public function __construct(){
$this->group = new ArrayCollection();
}
/**
* @var int
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var string
*
* @ORM\Column(name="teamname", type="string", length=255)
*/
private $team;
//[setters and getters here]
为了让所有的团队在我将不得不查询 groups_team
table.i将直接在mysql中查询表,但在symfony中,我必须这样做
in order to get all the teams in a group i would have to query the groups_team
table.i would have directly queried the table in just mysql but in symfony i have to do this
$groups = $em->getRepository("AppBundle\Model\Entity\Groups")->findBy(array('tournament' => $tournament->getId()));
//get all teams with group id in groups_team table
foreach ($groups as $group) {
$teamsingroup = $em->getRepository("AppBundle\Model\Entity\Team")->createQueryBuilder('o')
->innerJoin('o.group', 't')
->where('t.id = :group_id')
->setParameter('group_id', $group->getId())
->getQuery()->getResult();
echo "</b>".$group->getGroupname()."</b></br>";
foreach ($teamsingroup as $teamingroup) {
echo $teamingroup->getTeam()."</br>";
}
}
有人可以向我解释如何 innerJoin
正在工作,这背后的概念是什么,也许是一些了解这方面的文档。有更好的方法来做到这一点与symfony和教义。
Can someone explain to me how the innerJoin
is working and what is the concept behind this, maybe a few documentation to learn about this. are there better way to do this with symfony and doctrine.
推荐答案
在2个实体之间使用 ManyToMany
涉及第三个表,通常称为当您构建DQL(原则查询)原则时,您可以根据您定义为注释的关系的性质自动加入连接表,以便在考虑到您的查询
Using ManyToMany
between 2 entities involves a third table generally called as a junction table in this type of relation when you build a DQL (doctrine query) doctrine automatically joins junction table depending on the nature of relation you have defined as annotation so considering your query
$teamsingroup = $em->getRepository("AppBundle\Model\Entity\Team")
->createQueryBuilder('o')
->innerJoin('o.group', 't')
您正在加入团队
实体与组
实体在 innerJoin('o.group')
部分 o
是Team实体的别名, o.group
是指在中定义的属性团队
实体名为组
。
You are joining Team
entity with Group
entity in innerJoin('o.group')
part o
is the alias for Team entity and o.group
refers to property defined in Team
entity named as group
.
/**
* @ORM\ManyToMany(targetEntity="Groups", mappedBy="team")
*/
protected $group;
其中有一个 ManyToMany
为此定义的注释关系原则的类型首先使用连接表连接您的团队表,然后将您的连接表与组表连接,并且生成的SQL将类似于
Which has a ManyToMany
annotation defined for this type of relation doctrine joins your team table first with junction table and then joins your junction table with groups table and the resultant SQL will be something like
SELECT t.*
FROM teams t
INNER JOIN junction_table jt ON(t.id = jt.team_id)
INNER JOIN groups g ON(g.id = jt.group_id)
WHERE g.id = @group_id
createQueryBuilder 部分排除在一起,将团队属性定义为 ArrayCollection
ie $ this-> team = new ArrayCollection();
在每个组对象上,您将获得与该特定组关联的团队集合调用 getTeam()
函数对组件对象类似于下面的代码。
Another thing related your way of getting team for each group you can minimize your code by excluding createQueryBuilder
part within loop, once you have defined teams property as ArrayCollection
i.e $this->team = new ArrayCollection();
on each group object you will get collections of teams associated to that particular group by calling getTeam()
function on group object similar to below code.
foreach ($groups as $group) {
$teamsingroup = $group->getTeam();
echo "</b>".$group->getGroupname()."</b></br>";
foreach ($teamsingroup as $teamingroup) {
echo $teamingroup->getTeam()."</br>";
}
}
这篇关于内部联盟如何使用Doctrine和Symfony2在多对多关系中工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!