它可以自己加入单个表吗?

时间:2014-11-24 13:34:58

标签: php mysql sql codeigniter

这是模型函数我正在尝试加入单个数据库表它自己,parent :: get方法从表中获取数据!

public function get_with_parents($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);
}

这是控制器!!

$this->data ['pages'] = $this->Page_model->get_with_parents();

2 个答案:

答案 0 :(得分:0)

假设您的单个表名为pages,这应该有效 -

$this->db->select('p1.title as ptitle','p.slug as parent_slug','p.title as parent_title');
$this->db->from('pages AS p1');
$this->db->join('pages AS p','p1.parent_id=p.id','left');

答案 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))
   ->join('pages as p','pages.parent_id=p.id','left');
   return parent::get($id,$sengle);
}

public function get_with_parent ()
{
   $this->db->select('pages.*, p.slug as parent_slug, p.title as parent_title')
   ->where("pages.language", $this->uri->segment(1))
   ->join('pages as p', 'pages.parent_id=p.id', 'left');
   $query = $this->db->get('pages');
   return $query->result();
}