I have an entity called Category with the following fields:
id: integer
name: string
slug: string
children: OneToMany(targetEntity="Category", mappedBy="parent")
parent: ManyToOne(targetEntity="Category", inversedBy="children")
As you can see, each Category can be a child\parent of another.
I need to map all the categories to an array, so I figured it will be best to just load them all together:
$categoryRepository->findAll();
Later on, on the mapping method, I want to get each Category's children, so I use
$category->getChildren();
I assumed that since I loaded all the categories in the first place, only 1 query will be executed, but the profiler shows differently! There is 1 query where it fetches all the records (output from the profiler):
SELECT t0.id AS id1, t0.name AS name2, t0.slug AS slug3, t0.parent_id AS parent_id4
FROM acme_category t0
And then there is another query for every record:
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..
Why is that? How can I make it load all the records from the first place?
Thanks!