无法在codeigniter中加入

时间:2014-05-07 00:26:48

标签: php mysql sql codeigniter

我的表格结构'pages'

id int(11)
title varchar(100)
slug varchar(100)
order varchar(100)
body text
parent_id int(11)  (0/1)

我正在尝试将表格加入到自身中,以便我可以获得包含父母的页面(即parent_id1)。我在我的页面模型中创建了这个方法,该模型扩展了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();
}

2 个答案:

答案 0 :(得分:0)

您在查询中错过了发件人

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);
}

答案 1 :(得分:0)

public function get_with_parent($ id = NULL,$ sengle = FALSE)   {

$ this-> db-> select('pages。*,p.slug as parent_slug,p.title as parent_title')

- > where(“pages.language”,$ this-> uri-> segment(1))

- >加入('pages as p','pages.parent_id = p.id','left');

return parent :: get($ id,$ sengle);

}