azad6026
(Azad6026)
1
Hi there.I am new to yii.I am trying to figure out how to display a hierarchical menu in yii.
I read this article managing-hierarchical-data-in-mysql from mikehillyer
and now I want to implement it in yii. My proble is about table structure.In mtreeview
which I tried I don’t know which schema it uses for
its table and more importantly how to poplulate it. If it was only rgt,lft and id and name columns
like the article that I read it was fine , but it has position and parentid and id which confuses me.
Please help me in its table structure and its population.Thank you.
konapaz
(Konapaz)
2
azad6026
(Azad6026)
3
Thank you for your reply.I tried it but did not display anything to me.My problem is still there.I populated
the table with the same value for both id and parent_id is that correct?
What is wrong?what should I do?
konapaz
(Konapaz)
4
Please post your code
also you could var_dump your results to check what data exist
azad6026
(Azad6026)
5
this is my table scheme:
CREATE TABLE if not exists categories (
`id` int(11) NOT NULL AUTO_INCREMENT,
`parent_id` int(11) DEFAULT NULL,
`title` varchar(45) NOT NULL,
PRIMARY KEY (`id`),
FOREIGN KEY (`parent_id`) REFERENCES `categories` (`id`)
);
this is the controller:
private static $menuTree = array();
public static function getMenuTree() {
if (empty(self::$menuTree)) {
$rows = Categories::model()->findAll('parent_id IS NULL');
foreach ($rows as $item) {
self::$menuTree[] = self::getMenuItems($item);
}
}
return self::$menuTree;
}
private static function getMenuItems($modelRow) {
if (!$modelRow)
return;
if (isset($modelRow->Childs)) {
$chump = self::getMenuItems($modelRow->Childs);
if ($chump != null)
$res = array('label' => $modelRow->title, 'items' => $chump, 'url' => Yii::app()->createUrl('yourcontroller/youraction', array('id' => $modelRow->id)));
else
$res = array('label' => $modelRow->title, 'url' => Yii::app()->createUrl('yourcontroller/youraction', array('id' => $modelRow->id)));
return $res;
} else {
if (is_array($modelRow)) {
$arr = array();
foreach ($modelRow as $leaves) {
$arr[] = self::getMenuItems($leaves);
}
return $arr;
} else {
return array('label' => ($modelRow->title), 'url' => Yii::app()->createUrl('yourcontroller/youraction', array('id' => $modelRow->id)));
}
}
}
this is the view:
$this->widget('zii.widgets.CMenu', array(
'items' => $this->getMenuTree(),
));
this is what i added to the model in relations:
'Childs' => array(self::HAS_MANY, 'Categories', 'parent_id'),
the output is empty.
Thank you.
konapaz
(Konapaz)
6
trace the code at the important lines using var_dump.