I have some table with ids, parent ids, and names. When I iterating through table I would like to build one string with names when id has parent_id. For example id = 6. It has parent 2, and id 2 has parent 1, so string should be six / two / one
+---+---------+-------+
|id |parent_id| name |
+---------------------+
|1 | null | one |
|2 | 1 | two |
|3 | null | three |
|4 | 3 | four |
|5 | 4 | five |
|6 | 2 | six |
|7 | 3 | seven |
|8 | 2 | eight |
+---+---------+-------+
Below is my code. It is symfony2 code
public function getParentTaskTree($id){
$i = 10;
$parentTaskTree = '';
$taskRepository = $this->getEntityManager()
->getRepository('FrontendBundle:Task');
$task = $taskRepository->findOneBy(array('id' => $id));
if($task){
$parentTaskTree .= $task->getParent();
$parentTaskTree .= ' / ';
$this->getParentTaskTree($taskRepository->findOneBy(array('parent' => $task->getParent())));
}
return $parentTaskTree;
}