Yii中二级分类设置关系
/**
* @return array relational rules.
*/
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(
'Parent' => array(self::BELONS_TO, 'Your table name', 'parent_id'),
'Children' => array (self::HAS_MANY, 'Youre table name', 'parent_id')
);
}
$criteria = new CDbCriteria;
$criteria -> select = 'id,name';
$criteria -> limit = 15;
$criteria -> condition = 'parent_id = 0';
$categories = CtCategory::model()->findAll($criteria);
echo $categories->Parent->name;// 输出父级name
//
// 遍历children
//
foreach ($categories->Children as $child)
{
echo $child->name; // 输出子级name
}