我有一个名为Category的实体,其中包含以下字段:
id: integer
name: string
slug: string
children: OneToMany(targetEntity="Category", mappedBy="parent")
parent: ManyToOne(targetEntity="Category", inversedBy="children")
如您所见,每个类别都可以是另一个类别的子\父级。
我需要将所有类别映射到一个数组,所以我认为最好将它们全部加载在一起:
$categoryRepository->findAll();
稍后,在映射方法上,我想获取每个类别的孩子,所以我使用
$category->getChildren();
我假设由于我首先加载了所有类别,因此只会执行 1 个查询,但分析器显示不同!有1 个查询获取所有记录(分析器的输出):
SELECT t0.id AS id1, t0.name AS name2, t0.slug AS slug3, t0.parent_id AS parent_id4
FROM acme_category t0
然后对每条记录进行另一个查询:
SELECT t0.id AS id1, t0.name AS name2, t0.slug AS slug3, t0.parent_id AS parent_id4
FROM omnt_work_category t0 WHERE t0.parent_id = 1
SELECT t0.id AS id1, t0.name AS name2, t0.slug AS slug3, t0.parent_id AS parent_id4
FROM omnt_work_category t0 WHERE t0.parent_id = 1
etc..
这是为什么?我怎样才能让它从一开始就加载所有记录?
谢谢!