
Originally Posted by
pht
Hey
Could someone tell me advantage of nested set over next table schema:
DB:cats
cat_id
cat_title
cat_path
cat_level
* Not sure all RDBMS use indexes with LIKE.
* Operations like getting the parent or ancestors seems tricky.
Edit:
Doh, parent is fairly trivial, just popping of a bit of the path
Getting the ancestors of a particular category can be quite important for rendering stuff like breadcrumbs. Can't think of "nice" method doing that with paths atm.
Code:
SELECT p.categoryID AS "categoryID", p.name AS name, p.depth AS depth
FROM Categories AS p
INNER JOIN Categories AS c ON c.left > p.left AND c.left < p.right
WHERE c.categoryID = :categoryID ORDER BY p.left ASC
Is the nested set method, a simple bit of PHP to render..
Code:
$categories = $selectedCategory->getAncestors();
echo '<ol class="breadcrumb">';
foreach($categories as $category)
echo '<li>', SEP, '<a href="?categoryID=', htmlspecialchars($category->categoryID), '">', htmlspecialchars($category->name), '</a>', '</li>';
echo '<li>', SEP, '<a href="?categoryID=', htmlspecialchars($selectedCategory->categoryID), '">', htmlspecialchars($selectedCategory->name), '</a>', '</li>';
echo '</ol>';
Bookmarks