2014-11-24 60 views
0

這是我試圖加入單個數據庫表的模型函數它自己,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(); 
+0

'左' 的左加入 – 2014-11-24 13:38:30

+0

您也可以使用原始SQL – johnny 2017-04-17 16:54:17

回答

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'); 
+0

發生數據庫錯誤 錯誤編號:1066 不是唯一表/別名:'pages' SELECT'pages'。* FROM('pages' AS pages,'pages')LEFT JOIN'pages' as p ON'pages'.'parent_id' ='p' .'id' ORDER BY'order' 文件名:E:\ WAMP \ WWW \ CodeIgniter_2.2.0 \ SYSTEM \數據庫\ DB_driver.php 行號:330 – 2014-11-24 13:44:16

+0

更新的代碼,然後重試 – hashbrown 2014-11-24 13:49:22

+0

消息:未定義的屬性: stdClass :: $ parent_slug – 2014-11-24 14:18:17

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(); 
} 
相關問題