Self Relations

Hey guys,

is it possible to define relations within one model class. For example: A model class for menu items, those item can have a parent item, that is also an instance of MenuItems.

So one model instance can have one parent item and many other model instances can have this model as a parent item.

Is this possible?

I hope it got clear. Otherwise I will do a picture :)

Thanks

Tim

Hi seamus_d,

The relations should be something like the following:




// in MenuItem.php


public function relations()

{

    return array(

        'parentMenuItem' => array(self::BELONGS_TO, 'MenuItem', 'parent_id'),

        'childMenuItems' => array(self::HAS_MANY, 'MenuItem', 'parent_id'),

    );

}



But I think you would be better look into this extension first.

http://www.yiiframework.com/extension/nestedsetbehavior

It will help you in managing your hierarchical menu systems.