News使用一對(duì)多的自引用方法相互關(guān)聯(lián)(一個(gè)消息是父消息,可以有多個(gè)子消息)。更重要的是 each與和News具有正常的(非自引用的)一對(duì)一關(guān)系。當(dāng)我運(yùn)行簡(jiǎn)單的 DQL 時(shí):EventGallerySELECT n FROM App\Entity\News n WHERE n.parent = :id然后通過(guò)設(shè)置getResults默認(rèn)值的方法對(duì)結(jié)果進(jìn)行水合HYDRATION_OBJECT,在方法內(nèi)部的某處進(jìn)行額外的查詢getResults。SELECT t0.* FROM event t0 WHERE t0.news_id = 2 AND ((t0.deleted_at IS NULL));SELECT t0.* FROM gallery t0 WHERE t0.news_id = 2 AND ((t0.deleted_at IS NULL));SELECT t0.* FROM event t0 WHERE t0.news_id = 1 AND ((t0.deleted_at IS NULL));SELECT t0.* FROM gallery t0 WHERE t0.news_id = 1 AND ((t0.deleted_at IS NULL));第一次查詢選擇的新聞的孩子在哪里和在哪里news_id = 1。news_id = 2News也沒(méi)有自引用的一對(duì)多關(guān)系(我在這里忽略了它們),但是 hydration 沒(méi)有對(duì)它們進(jìn)行額外的查詢。parent僅當(dāng)語(yǔ)句中涉及關(guān)系時(shí)才會(huì)出現(xiàn)此問(wèn)題where。如何重現(xiàn)// news Entity/** * @ORM\Entity(repositoryClass="App\Repository\NewsRepository") * @ORM\Table(uniqueConstraints={@UniqueConstraint(name="news_slug_deleted", columns={"slug","deleted_at"})}) *class News { use SoftDeleteableEntity; /** * @ORM\Id() * @ORM\GeneratedValue() * @ORM\Column(type="integer") */ private $id; /** * @ORM\Column(type="string", length=255) */ private $title; /** * @Gedmo\Slug(fields={"title"}) * @ORM\Column(length=128) */ private $slug;/** * @ORM\OneToOne(targetEntity="App\Entity\Gallery", mappedBy="news", cascade={"persist", "remove"}, orphanRemoval=true) * * @var Gallery */ private $gallery; /** * @ORM\OneToOne(targetEntity="App\Entity\Event", mappedBy="news", cascade={"persist", "remove"}, orphanRemoval=true) * * @var Event */ private $event; /** * One News has Many News. * @ORM\OneToMany(targetEntity="News", mappedBy="parent") */ private $children; /** * Many News have One News. * @ORM\ManyToOne(targetEntity="News", inversedBy="children") * @ORM\JoinColumn(name="parent_id", referencedColumnName="id", nullable=true) */ private $parent;}
1 回答

慕娘9325324
TA貢獻(xiàn)1783條經(jīng)驗(yàn) 獲得超4個(gè)贊
您在該實(shí)體中有幾個(gè)反向 OneToOne 關(guān)系。
反向 OneToOne 關(guān)系不能被 Doctrine 延遲加載,并且很容易成為性能問(wèn)題。
如果您真的需要將這些關(guān)系映射到反面(而不僅僅是在擁有方),請(qǐng)確保明確地進(jìn)行適當(dāng)?shù)倪B接,或者將這些關(guān)聯(lián)標(biāo)記為以便FETCH=EAGERDoctrine 為您進(jìn)行連接。
例如,避免可怕的“n+1”問(wèn)題的查詢是:
SELECT n, g, e
? ? FROM App\Entity\News n
? ? LEFT JOIN n.gallery g
? ? LEFT JOIN n.event e
WHERE n.parent = :id
- 1 回答
- 0 關(guān)注
- 138 瀏覽
添加回答
舉報(bào)
0/150
提交
取消