我的表结构'pages'
:
id int(11)
title varchar(100)
slug varchar(100)
order varchar(100)
body text
parent_id int(11) (0/1)
我试图加入一个表到自己,这样我就可以得到有父母的页面(即parent_id
与1
)。我在扩展MY_Model
的页面模型中使用了此方法,但它不起作用
public function get_with_parent ($id = NULL, $single = FALSE)
{
$this->db->select('pages.*, p.slug as parent_slug, p.title as parent_title');
$this->db->join('pages as p', 'pages.parent_id=p.id', 'left');
return parent::get($id, $single);
}
MY_Model:
public function get($id = NULL, $single = FALSE)
{
if($id !== NULL) {
$filter = $this->_primary_filter;
$id = $filter($id);
$this->db->where($this->_primary_key, $id);
$method = "row";
} elseif($single) {
$method = "row";
} else {
$method = "result";
}
if(!count($this->db->ar_order)) {
$this->db->order_by($this->_order_by);
}
return $this->db->get($this->_tablename)->$method();
}
你的查询中漏掉了From
public function get_with_parent ($id = NULL, $single = FALSE)
{
$result = $this->db->select('pages.*, p.slug as parent_slug, p.title as parent_title')
->from('pages');//here you need to put this code
->join('pages as p', 'pages.parent_id=p.id', 'left');
return parent::get($id, $single);
}
get_with_parent($id = NULL,$ single = FALSE){
$ this -> db ->选择"页面。*,p。Slug作为parent_slug,p。标题为parent_title')
->,("页面。语言",$ this -> uri ->段(1))
->加入(页面和p, pages.parent_id = p.id ', '左');
返回父:获得(id、sengle美元);
}