我想获取所有类别和子类别的列表enabled=1
类别关系:
return array(
'subcategories'=>array(self::HAS_MANY, 'Subcategories', 'id','condition'=>'enabled=1'),
);
子类别中的关系:
return array(
'categories'=>array(self::BELONGS_TO, 'Categories', 'category_id', 'condition'=>'enabled=1'),
);
这是我想做的事情:
$categories = Categories::model()->with('subcategories')->findAll();
$selectData = array();
foreach ($categories as $category) {
$selectData[$category->title] = CHtml::listData($category->subcategories, 'id', 'title');
}
echo CHtml::dropDownList('catAndSubcat', null, $selectData);
两个表都有列enabled
。每个类别都有近 10 个子类别。
在子类别中是类别category_id
中的外键id
但我收到错误:
SQLSTATE[23000]: Integrity constraint violation: 1052 Column 'enabled' in where clause is ambiguous. The SQL statement executed was: SELECT `t`.`id` AS `t0_c0`, `t`.`title` AS `t0_c1`, `t`.`page_id` AS `t0_c2`, `t`.`position` AS `t0_c3`, `t`.`enabled` AS `t0_c4`, `subcategories`.`id` AS `t1_c0`, `subcategories`.`category_id` AS `t1_c1`, `subcategories`.`title` AS `t1_c2`, `subcategories`.`page_id` AS `t1_c3`, `subcategories`.`position` AS `t1_c4`, `subcategories`.`enabled` AS `t1_c5` FROM `t_categories` `t` LEFT OUTER JOIN `t_subcategories` `subcategories` ON (`subcategories`.`id`=`t`.`id`) WHERE (enabled=1)
帮我!