Recursive(hierarchical) Structure

Hello! I totaly confused after i read this articles

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

http://www.yiiframework.com/doc/cookbook/61/

and now i want to understant, how to create array for CTreeView or DropDownList from this hierarchical structure ?

i have table




    CREATE TABLE IF NOT EXISTS `Place` (

      `id` int(11) NOT NULL AUTO_INCREMENT,

      `sort` int(11) NOT NULL,

      `parent` int(11) DEFAULT NULL,

      `title` varchar(255) DEFAULT NULL,

      PRIMARY KEY (`id`)

    ) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=8 ;

     

And model Place.php with this functions


  public function relations()

        {

            // NOTE: you may need to adjust the relation name and the related

            // class name for the relations automatically generated below.

             return array(

          'getparent' => array(self::BELONGS_TO, 'Place', 'parent'),

          'childs' => array(self::HAS_MANY, 'Place', 'parent', 'order' => 'sort ASC'),

        );

        }

I understand this thing -




Parent name of this id = 7

$model = Place::model()->findByPk(7);

$parent = $model->getparent;

echo $parent->title;




All childrens from pk1

$model = Place::model()->findByPk(1);

foreach ($model->childs as $child)

{

    echo '<br />'.$child->title;


} 

But i need to create array, that will work with my widgets, like this


    $this->widget('CTreeView', array('data' => $items)); 

or some widgets dropdownlists …

I dont know how =(((

IS there any decisions ?

thanx…

ps Sory for english…

Here is how I am doing it. Its not the most perfect way.

In the model Place.php




	public function listing(){

		$models = Place::model()->findAll('parent IS NULL');

		foreach($models as $model) {

			$row['text'] = CHtml::link($model->title, array('place/view', 'id' => $model->id));

			$row['children'] = Place::getChilds($model->id);

			$data[] = $row;

		}	

		return $data;

	}


	public function getChilds($id) {

		foreach(Place::model()->findAll('parent = ' . $id) as $model) {

			$row['text'] = CHtml::link($model->title, array('place/view', 'id' => $model->id));

			$row['children'] = Place::getChilds($model->id);

			$data[] = $row;

		}

		return $data;

	}



And then in the view:

$this->widget(‘CTreeView’, array(‘data’ => Place::listing()));

How many levels this functions retrieve?

Ex:


Root

 |

 |-level 1

 |   |

 |   |-level 1.1

 |   /

 |-level 2

 /   |

     |-level 2.1

     /   |

         |-level 2.1.2

          /

This functions can retrieve the 3 levels shown above?

old post :lol: ; still can help some one . the getChilds is an recursive method , it will visit all tree , but we can add another variable to control the depth.

                                     protected  &#036;depth = 0;

and in the getChilds method increase it if it greater than a number(you can add another param to getChilds: getChilds($id,$depth=false)) // if you give a number this var can compared with $depth; if you call it many times make sure reset $depth to 0;