I am using Symfony 2.8 with doctrine 2.4
I have an entity with One-To-Many, Self-referencing Association Mapping
entity:
id
name
parent_id
example:
id: 1
name: A
parent_id: null
id: 2
name: B
parent_id = 1
id: 3
name: C
parent_id = 2
id: 4
name: D
parent_id = 3
I wrote a function which finds all nested children's id of the parent.
public function getAllChildrenIdRecursively($entity)
{
$result = array();
if (count($entity->getChildren()) > 0) {
$result[] = $entity->getId();
foreach ($entity->getChildren() as $child) {
$result = array_merge($result, $this->getAllChildrenIdRecursively($child)) ;
}
}
return $result;
}
if i call this function getAllChildrenIdRecursively(2) then i will get 3, 4.
this function is working but my problem is it generates too many queries in DB which take almost 1.5 seconds.Query generates more than 2500, total result = 2600 and total row = 5000
How can I reduce query time?
Any help would be much appreciated! Thanks