I'm currently developing an extension for zend framework that helps to
manage with tree structures in databases. I need a couple advices about my
architecture. All additional thoughts and ideas are welcome as well.
I have already done an extension that helps to work with Nested Sets trees.
Here are my classes (just to let you imagine my structure):
System_Db_Tree_Nested – main class, that returns Zend_Db_Select objects that
can be joined with data tables to get data.
System_Db_Tree_Nested_Adapter_Abstract – abstract adapter (adapters are used
for making db queries)
System_Db_Tree_Nested_Adapter_Interface – interface for adapters (I made it
public, just in case, you see it here: http://pastie.org/510819
System_Db_Tree_Nested_Adapter_Pdo_Abstract – abstract adapter for pdo
System_Db_Tree_Nested_Adapter_Pdo_Mysql – adapter for pdo_mysql
System_Db_Tree_Nested_Table – descendant of Zend_Db_Table_Abstract
System_Db_Tree_Nested_Table_Row – special row class for usage in tables with
tree structure. (I made it public too, check it here:
http://pastie.org/private/lidc63k81n7toscnioog)
System_Db_Tree_Nested_Exception just exception
***
That's it. The most common way of usage my extension is to create a table
like this:
<?php
class System_DbTable_Menu extends System_Db_Tree_Nested_Table
{
/**
* Name of the table
*
* $var string
*/
protected $_name = 'menu_items';
/**
* Nested tree id for the table
*
* @var integer
*/
protected $_nestedTreeId = null;
}
And you can use it like usual Zend_Db_Table object:
$menu = new Menu();
$menuItem = $menu->fetchRow('id=2');
or use special methods provided by my System_Db_Tree_Nested_Table:
$menuItems = $menu->fetchTree();
$menuItem = $menu->fetchRow('id=2');
$menuItem->moveToNext();
$childrenMenuItems = $menu->fetchDescendants($menuItem);
$menu->insertChildLast(array('title'=>'text'), $menuItem);
and so on and so on.
That was an introducation.
My question is about returning data. Now, rowset can be like this:
id, title, level
================
7 text 1
3 label 1
6 item 2
4 check 3
5 test 1
i.e we have levels and can build our structure (on the page or somewhere
else, may be menu or categories tree) with these levels. Like this:
text
label
** item
**** check
test
And everything is fine and it works. But I think that it is not very
universal and fits only for nested sets algorithm. I'm planning to create
also implementations for adjacent list and materialized path algorithms. And
I think it is a good idea to universalize returning data. How would you do
that?
For instance, I was thinking about nested array format alike to one that is
used in zend navigator.
i.e. :
array (
'data' => (some data, may be an array or zend_db_row object)
'elements' => 'an array (or may be rowset... o_O) with descendants of this
element)'
...
)
What do you think about that approach? I think that usage of
Zend_Db_Table_Rowset and Zend_Db_Table_Row like descendants to work with my
tree items is very comfortable, because I still have all advantages of
these classes. But I'm not sure that they can be easily used with tree
structured data.
I have also implemented a function to convert my data (with levels yet) to
zend_navigator object and I'm finding it very useful. But I'm not sure where
to put this function. May be, I could make an action helper or something
like that? How do you think?
Thanks.
--
View this message in context: http://www.nabble.com/architect-help-needed-tp24015121p24015121.html
Sent from the Zend DB mailing list archive at Nabble.com.
没有评论:
发表评论