On Monday 27 July 2009 10:33:29 Nick Pack wrote:
> Hey All,
>
> I recently added a new proposal draft to the wiki for a new component, I
> am attempting to get out of the box support for the modified preorder
> tree traversal algorithm, I have some working code kindly provided by
> Hector Virgen, that does basically what I am proposing (it just needs
> refinement) and am just after some feedback on the proposal from the
> community.
>
> The proposal is located at:
> http://framework.zend.com/wiki/display/ZFPROP/Zend_Db_Table_Mptt+-+Nick+Pac
>k
>
> An excellent article on what MPTT is and how you use it is at (for those
> of you that have no idea what MPTT is!):
> http://www.sitepoint.com/article/hierarchical-data-database/
>
> I have a couple of questions with regards to its design that I would
> like some feedback on.
>
> 1. Should the top level parent node have parent_id values of 0 or NULL,
> from a normalisation perspective, one would guess that the parent should
> be 0 so that all values in the col are of the same type, however by
> using 0 you are effectively creating a reference to a non-existant row -
> any feedback on this behavior would be greatly appreciated

If we're talking DB, you must use null to comply to DB FK restrictions. If 
we're talking code, 0 is fine.

> 2. This question is with regards to handling deletion of nodes that have
> children, it would seem much more sensible to move the child nodes of
> the deleted node up to the grandparent node, rather than breaking the
> chain of parent references or deleting the child nodes completely, is
> this the best way to approach it?

You can mimick the FK behavior: MAKE NULL, RESTRICT and CASCADE. I'd add 
REATTACH here and you should be set.

like this:

// make all direct children root nodes, retaining their subtrees
$table->delete($id, Zend_Db_Table_Mptt::DELETE_MAKE_NULL);

// restrict deleting if child nodes exists
if ($table->delete($id, Zend_Db_Table_Mptt::DELETE_RESTRICT)) {
        // delete successful
} else {
        // delete failed
}

// delete the node and all it's child nodes recursively
$table->delete($id, Zend_Db_Table_Mptt::DELETE_CASCADE);

// attach all nodes direct children to it's parent, making them siblings
$table->delete($id, Zend_Db_Table_Mptt::DELETE_REATTAC);

// attach all nodes direct children to node with ID $newParent
$table->delete($id, Zend_Db_Table_Mptt::DELETE_REATTACH, $newParent);

These are all just examples, but you should get the gist. :)

-- 
Dado

Reply via email to