学说双向多对多

时间:2014-12-09 13:27:46

标签: php symfony doctrine-orm doctrine

我在“群组”和“检查”之间存在多对多关系。

数据库表:

+----------------+    +----------------+    +----------------+
|  GROUP         |    |  CHECKFOR      |    |  CHECK         |
+----------------+    +----------------+    +----------------+
|  id            |    |  group         |    |  id            |
|  name          |    |  check         |    |  description   |
|  next          |    |                |    |  author        |
+----------------+    +----------------+    +----------------+

Symfony学说实体

<?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;

    ...

}

检查

<?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;

     ...
}

运行时,..->repository->findBy()会创建一个无限循环。如何防止这个'错误'?

日志:

[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] []

更新: 我发现了问题! 'Checks'是持久收集而不是数组收集。 持久收集对JMS序列化程序不起作用。当我打印组对象的转储时,我在dev_log(= correct)中看到2个查询。当我用序列化器解析对象时,我看到+10个查询(=不好)。

1 个答案:

答案 0 :(得分:0)

Doctrine ManyToMany实际存在,因此您无需创建第三个实体。它会自动创建。

关键字是@ORM \ JoinTable

http://doctrine-orm.readthedocs.org/en/latest/reference/association-mapping.html#many-to-many-bidirectional