スラッグを使用してこのテーブルから結果を取得しようとすると、問題が発生します。
| id | parent | slug | name |
-----------------------------------------
| 1 | 0 | animations | animations |
| 2 | 1 | flash | flash |
| 3 | 2 | looped | looped |
| 4 | 1 | gif | gif images |
たとえば、親が「アニメーション」で子が「フラッシュ」であるカテゴリを取得する必要があります。
本当の問題は、カテゴリ/$parent_slug/$child_slug を使用して結果を検索する必要があるためです。代わりに ID (category/$id) を使用して取得します|3|2|looped|looped|
。
これは私がこれまでに行ったことです:
function get_category_childrens($category_parent=null){
$this->db->select('*');
if(!is_null($category_parent)){
$this->db->where('categories.slug', $category_parent);
$this->db->join('categories as l1', 'l1.parent = categories.id', 'left');
}
else{
$this->db->where('categories.parent', '0');
}
$query = $this->db->get('categories');
return $query->result_array();
}
生成されたSQL:
SELECT *
FROM (`categories`)
LEFT JOIN `categories` as l1 ON `l1`.`parent` = `categories`.`id`
WHERE `categories`.`slug` = 'animations'
CI を知らなくても問題ありません。質問やアイデアがある場合は、コメントしてください。