Kod
id | tree_left | tree_right | user_id
----+-----------+------------+---------
10 | 1 | 14 | 3
16 | 2 | 7 | 3
20 | 3 | 4 | 3
24 | 5 | 6 | 3
21 | 8 | 9 | 3
22 | 10 | 11 | 3
23 | 12 | 13 | 3
----+-----------+------------+---------
10 | 1 | 14 | 3
16 | 2 | 7 | 3
20 | 3 | 4 | 3
24 | 5 | 6 | 3
21 | 8 | 9 | 3
22 | 10 | 11 | 3
23 | 12 | 13 | 3
A więc takie drzewo:
Kod
10
-- 16
-- -- 20
-- -- 24
-- 21
-- 22
-- 23
-- 16
-- -- 20
-- -- 24
-- 21
-- 22
-- 23
Gdzie user_id to tree_scope
Posiłkując się dokumentacją stworzyłem iterator:
Kod
class TreeDisplayIterator extends RecursiveIteratorIterator
{
public function __construct($m){parent::__construct($m, self::SELF_FIRST );}
public function beginChildren(){echo '<ul>';}
public function endChildren() {echo '</ul>';}
}
{
public function __construct($m){parent::__construct($m, self::SELF_FIRST );}
public function beginChildren(){echo '<ul>';}
public function endChildren() {echo '</ul>';}
}
Wszystko jest w porządku jeśli chcę wyświetlać całe drzewo:
Kod
<ul>
<?php foreach(new TreeDisplayIterator(TreePeer::retrieveTree(3)) as $elem): ?>
<li><?php echo $elem->getTitle() ?></li>
<?php endforeach ?>
</ul>
<?php foreach(new TreeDisplayIterator(TreePeer::retrieveTree(3)) as $elem): ?>
<li><?php echo $elem->getTitle() ?></li>
<?php endforeach ?>
</ul>
Lista jest kompletna, elementy dobrze zagnieżdżone a ja bardzo szczęśliwy. Nie wiem tylko jak wyświetlić określoną odnogę. Dla przykładu dla wpisu o ID 16 chciałbym uzyskać coś takiego:
Kod
16
-- 20
-- 24
-- 20
-- 24
Ale wszystkie próby zawiodły, próbowałem tak:
Kod
<ul>
<?php foreach(new TreeDisplayIterator(TreePeer::retrieveByPk(16)) as $elem): ?>
<li><?php echo $elem->getTitle() ?></li>
<?php endforeach ?>
</ul>
<?php foreach(new TreeDisplayIterator(TreePeer::retrieveByPk(16)) as $elem): ?>
<li><?php echo $elem->getTitle() ?></li>
<?php endforeach ?>
</ul>
Ale to nie to. Jak to zrobić bez zbędnego mnożenia zapytań do bazy?