Need example DB Nested Sets

Hi.

Has anyone coded Nested Sets in concept of Yii Framework? ??? Example is very needed. My brain is reaching it’s boiling point  :o

Thanks.

More details?

I need to create some kind of catalog and want to use nested sets technology(http://www.kamfonas.com/id3.html).

Traditional structure (id, parent_id) is too slow for deep items.

Maybe someone coded something like this?

I've heard about that type of tree system.  It's a great system for large databases I believe.  I do not think it is an easy thing to program.

CakePHP has this, perhaps it could be ported?

http://api.cakephp.o…php-source.html

In cakephp it is packaged as something called a behavior, which in Yii would translate to (I believe) a subclass of CActiveRecord of which application models could inherit from.

PEAR has a Tree package, that also can handled nested sets. I also already wondered, if it would make sense to have a class for tree manipulation in Yii. I think it could be very useful for working with js trees, but there’s already another topic about this. :)

I see. I actually know this algorithm very well. ;)

Actually this may not be the best solution to this kind of problem, although it is theoretically sounding.

There are many open source implementations. Maybe we can implement that too in Yii.

Quote

... I actually know this algorithm very well. ;)

Qiang,

Do you know publically available good reading wrt tree-like structures (I mean more practice rather theoratical reading)?

This book covers the topic in great depth:

Joe Celko's Trees and Hierarchies in SQL for Smarties (Morgan Kaufmann, 2004, 1-55860-920-2)

I was thinking, and actually a model could use tree functionality in a way like this:

<?php


class Model extends CActiveRecord


{


	public $tree;





	public function __construct() {


		$this->tree = new Tree(&$this);


	}


}


Then the tree methods can be accessed like this: $model->tree->method();

This way a model can have multiple behaviors at the same time, and not just one if it was to inherit it.

But this way you can not extend the methods within the model in an as-nice way.

Perhaps it would be better to use the built-in event system and implement behaviours such as in RoR. 


<?php


class Model extends CActiveRecord


{


   public $acts_as=array('nested_set'=>array('scope_column'=>'scope'),


                                 'versioned'=>array('columns'=>array('description','post'), 'version_table'=>'versions');





}


These behaviours are loaded by the constructor and act when events (onBeforeSave,onBeforeUpdate) happen. These events coexist with the overridable methods available right now in the ActiveRecord classes. Behaviour methods (getChildren(), getVersion($id)) can be transparently mapped to with a __call method in the ActiveRecord base class.

Quite some work to get this all working though, however using the events in conjunction with the ActiveRecord base class is quite feasible I think and make it easy to add simple observers to it.

Actually I believe that's the way cake does it too dalip.  Seems like a good solution to me.

Good discussion. I think we can implement the behavior pattern and probably add those events as well. My main concern is the performance and the usage complexity. Could you please create a ticket for this? At the same time, maybe you can show some prototype implementation here? Thanks.

Quote

This book ...
Thanks!

I just implemented the basic behavior support in CComponent. That is, any CComponent-based components can use behavior feature.

The next thing to do is to write a base behavior class specifically used by CActiveRecord. And also to define a scheme to let an AR class to declare behaviors.

Could you please help review the code?

http://code.google.c…ce/detail?r=548

I'll be looking at the code more, but this is how cake does it:

http://book.cakephp…Using-behaviors

In your current implantation, can you access the main object from within a behavior?

For instance, in cake, you would define behavior methods this way:

function children(&$model, $moreArguments, $arg3, …)

Then within the method you can access the model through $model.

When you actually called the method, you would call it like so though:

$model->children($moreArguments, $arg3, …)

Quote

This book covers the topic in great depth:

Joe Celko's Trees and Hierarchies in SQL for Smarties (Morgan Kaufmann, 2004, 1-55860-920-2)

Let me also add this:

http://www.sitepoint…ata-database/2/

Quote

Let me also add this:

http://www.sitepoint…ata-database/2/

Thanks!

Thanks for the comment. I just added Cbehavior::getComponent so that behavior objects can retrieve the component it is attached to.

The behavior feature is fully completed.

These base behavior classes are added: Cbehavior, CModelbehavior and CActiveRecordbehavior.

Both CModel and CActiveRecord are changed so that they raise events in those beforeXyz and afterXyz methods. behaviors can respond to these events by overriding the corresponding methods in their own classes.

Also, application components can be attached with behaviors now. Simply specify the behaviors in app config by setting the 'behaviors' property of the app components.

Comments and suggestions are welcome!

Quote

Both CModel and CActiveRecord are changed so that they raise events in those beforeXyz and afterXyz methods. behaviors can respond to these events by overriding the corresponding methods in their own classes.

Awesome!  I was going to suggest that.  I will test it when I can