[英]Doctrine bidirectional many to many
I have a bidirectional many to many relationship between "Groups" and "Checks". 我在“组”和“检查”之间存在双向多对多关系。
Database tables: 数据库表:
+----------------+ +----------------+ +----------------+
| GROUP | | CHECKFOR | | CHECK |
+----------------+ +----------------+ +----------------+
| id | | group | | id |
| name | | check | | description |
| next | | | | author |
+----------------+ +----------------+ +----------------+
Symfony doctrine entities Symfony学说实体
Group 组
<?php
class Group implements GroupInterface
{
/**
* @var integer
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var string
* @ORM\Column(name="name", type="string", length=32, nullable=false)
*/
private $name;
/**
* @ORM\OneToOne(targetEntity="Group")
* @ORM\JoinColumn(name="next", referencedColumnName="id")
* @Serializer\Exclude
*/
private $next;
/**
* @ORM\OneToMany(targetEntity="CheckFor", mappedBy="group")
*/
private $checks;
...
}
Check 校验
<?php
class Check implements CheckInterface
{
/**
* @var integer
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var string
* @ORM\Column(name="description", type="string", length=256)
*/
private $description;
/**
* @ORM\ManyToOne(targetEntity="\Jds\UserBundle\Entity\User")
* @ORM\JoinColumn(name="madeByUser", referencedColumnName="id")
* @Serializer\Exclude
*/
private $author;
/**
* @var string
* @ORM\Column(name="kind", type="string", length=20)
*/
private $kind;
/**
* @ORM\OneToMany(targetEntity="CheckFor", mappedBy="group")
*/
private $groups;
}
checkfor 检查
class CheckFor implements CheckForInterface
{
/**
* @ORM\ManyToOne(targetEntity="Check")
* @ORM\JoinColumn(name="check", referencedColumnName="id")
* @ORM\Id
*/
private $check;
/**
* @ORM\ManyToOne(targetEntity="Group")
* @ORM\JoinColumn(name="group", referencedColumnName="id")
* @ORM\Id
*/
private $group;
...
}
When running, an infinite loop is created by ..->repository->findBy()
. 运行时,将通过..->repository->findBy()
创建无限循环。 How to prevent this 'bug'? 如何防止这种“错误”?
Logs: 日志:
[2014-12-09 14:04:23] doctrine.DEBUG: SELECT t0.id AS id1, t0.name AS name2, t0.next AS next3 FROM the_group t0 [] []
[2014-12-09 14:04:23] doctrine.DEBUG: SELECT t0.check AS check1, t0.group AS group2 FROM checkfor t0 WHERE t0.group = ? [1] []
[2014-12-09 14:04:24] doctrine.DEBUG: SELECT t0.id AS id1, t0.title AS title2, t0.description AS description3, t0.kind AS kind4, t0.madeByUser AS madeByUser5 FROM the_check t0 WHERE t0.id = ? ["1"] []
[2014-12-09 14:04:24] doctrine.DEBUG: SELECT t0.check AS check1, t0.group AS group2 FROM checkfor t0 WHERE t0.group = ? [1] []
[2014-12-09 14:04:24] doctrine.DEBUG: SELECT t0.id AS id1, t0.title AS title2, t0.description AS description3, t0.kind AS kind4, t0.madeByUser AS madeByUser5 FROM the_check t0 WHERE t0.id = ? ["2"] []
[2014-12-09 14:04:24] doctrine.DEBUG: SELECT t0.check AS check1, t0.group AS group2 FROM checkfor t0 WHERE t0.group = ? [2] []
[2014-12-09 14:04:24] doctrine.DEBUG: SELECT t0.id AS id1, t0.title AS title2, t0.description AS description3, t0.kind AS kind4, t0.madeByUser AS madeByUser5 FROM the_check t0 WHERE t0.id = ? ["3"] []
[2014-12-09 14:04:24] doctrine.DEBUG: SELECT t0.check AS check1, t0.group AS group2 FROM checkfor t0 WHERE t0.group = ? [3] []
[2014-12-09 14:04:24] doctrine.DEBUG: SELECT t0.id AS id1, t0.title AS title2, t0.description AS description3, t0.kind AS kind4, t0.madeByUser AS madeByUser5 FROM the_check t0 WHERE t0.id = ? ["4"] []
[2014-12-09 14:04:24] doctrine.DEBUG: SELECT t0.check AS check1, t0.group AS group2 FROM checkfor t0 WHERE t0.group = ? [4] []
[2014-12-09 14:04:24] doctrine.DEBUG: SELECT t0.id AS id1, t0.title AS title2, t0.description AS description3, t0.kind AS kind4, t0.madeByUser AS madeByUser5 FROM the_check t0 WHERE t0.id = ? ["5"] []
[2014-12-09 14:04:24] doctrine.DEBUG: SELECT t0.check AS check1, t0.group AS group2 FROM checkfor t0 WHERE t0.group = ? [5] []
[2014-12-09 14:04:24] doctrine.DEBUG: SELECT t0.id AS id1, t0.title AS title2, t0.description AS description3, t0.kind AS kind4, t0.madeByUser AS madeByUser5 FROM the_check t0 WHERE t0.id = ? ["7"] []
[2014-12-09 14:04:24] doctrine.DEBUG: SELECT t0.check AS check1, t0.group AS group2 FROM checkfor t0 WHERE t0.group = ? [7] []
[2014-12-09 14:04:24] doctrine.DEBUG: SELECT t0.id AS id1, t0.title AS title2, t0.description AS description3, t0.kind AS kind4, t0.madeByUser AS madeByUser5 FROM the_check t0 WHERE t0.id = ? ["15"] []
[2014-12-09 14:04:24] doctrine.DEBUG: SELECT t0.check AS check1, t0.group AS group2 FROM checkfor t0 WHERE t0.group = ? [15] []
[2014-12-09 14:04:24] doctrine.DEBUG: SELECT t0.id AS id1, t0.title AS title2, t0.description AS description3, t0.kind AS kind4, t0.madeByUser AS madeByUser5 FROM the_check t0 WHERE t0.id = ? ["10"] []
[2014-12-09 14:04:24] doctrine.DEBUG: SELECT t0.check AS check1, t0.group AS group2 FROM checkfor t0 WHERE t0.group = ? [10] []
[2014-12-09 14:04:24] doctrine.DEBUG: SELECT t0.id AS id1, t0.title AS title2, t0.description AS description3, t0.kind AS kind4, t0.madeByUser AS madeByUser5 FROM the_check t0 WHERE t0.id = ? ["14"] []
[2014-12-09 14:04:24] doctrine.DEBUG: SELECT t0.check AS check1, t0.group AS group2 FROM checkfor t0 WHERE t0.group = ? [14] []
[2014-12-09 14:04:24] doctrine.DEBUG: SELECT t0.id AS id1, t0.title AS title2, t0.description AS description3, t0.kind AS kind4, t0.madeByUser AS madeByUser5 FROM the_check t0 WHERE t0.id = ? ["19"] []
[2014-12-09 14:04:24] doctrine.DEBUG: SELECT t0.check AS check1, t0.group AS group2 FROM checkfor t0 WHERE t0.group = ? [19] []
[2014-12-09 14:04:24] doctrine.DEBUG: SELECT t0.id AS id1, t0.title AS title2, t0.description AS description3, t0.kind AS kind4, t0.madeByUser AS madeByUser5 FROM the_check t0 WHERE t0.id = ? ["25"] []
[2014-12-09 14:04:24] doctrine.DEBUG: SELECT t0.check AS check1, t0.group AS group2 FROM checkfor t0 WHERE t0.group = ? [25] []
[2014-12-09 14:04:24] doctrine.DEBUG: SELECT t0.id AS id1, t0.title AS title2, t0.description AS description3, t0.kind AS kind4, t0.madeByUser AS madeByUser5 FROM the_check t0 WHERE t0.id = ? ["26"] []
[2014-12-09 14:04:24] doctrine.DEBUG: SELECT t0.check AS check1, t0.group AS group2 FROM checkfor t0 WHERE t0.group = ? [26] []
[2014-12-09 14:05:01] doctrine.DEBUG: SELECT t0.check AS check1, t0.group AS group2 FROM checkfor t0 WHERE t0.group = ? [2] []
[2014-12-09 14:05:21] doctrine.DEBUG: SELECT t0.check AS check1, t0.group AS group2 FROM checkfor t0 WHERE t0.group = ? [3] []
Update: I found the problem! 更新:我发现了问题! 'Checks' is a persistentcollection and not an arraycollection. “检查”是一个持久性集合,而不是一个数组集合。 The persistentcollection does not work well with the JMS serializer. 持久性收集不适用于JMS序列化器。 When I print a Dump of the Group Object i see 2 queries in the dev_log (=correct). 当我打印组对象的转储时,我在dev_log中看到2个查询(=正确)。 When I parse the object with the serializer i see +10 queries (= not good). 当我使用序列化器解析对象时,我看到+10个查询(=不好)。
Doctrine ManyToMany actually exists, so you don't need to create a third entity. 教义ManyToMany实际上存在,因此您不需要创建第三个实体。 it will be created automatically. 它将自动创建。
Keyword for this is @ORM\\JoinTable 关键字是@ORM \\ JoinTable
http://doctrine-orm.readthedocs.org/en/latest/reference/association-mapping.html#many-to-many-bidirectional http://doctrine-orm.readthedocs.org/en/latest/reference/association-mapping.html#many-to-many-bidirectional
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.