Codeigniter活动记录加入

时间:2015-04-20 09:18:39

标签: codeigniter

我有一个查询,用于从两个表中获取数据。我如何使用codeigniter活动记录"加入"而不是我的查询

public function get_categories($parent_id = 0) {       
    $query = $this->db->query("SELECT * FROM " . $this->db->dbprefix . "category c LEFT JOIN " . $this->db->dbprefix . "category_description cd ON (c.category_id = cd.category_id) WHERE c.status=1 and c.parent_id = '" . (int)$parent_id . "' order by sort_order");
    return $query->result_array();
}

问题:最好将它转换为codeigniter活动记录"加入"功能

2 个答案:

答案 0 :(得分:2)

试试这个

$this->db->select('*');
$this->db->from($this->db->dbprefix.'category c');
$this->db->join($this->db->dbprefix.'category_description cd', 'c.category_id = cd.category_id');
$this->db->where('c.status',1);
$this->db->where('c.parent_id',$parent_id);
$this->db->order_by("sort_order", "asc");
$query = $this->db->get();
return $query->result_array();

答案 1 :(得分:1)

$this->db->select('*');
$this->db->from($this->db->dbprefix.'category c');
$this->db->join($this->db->dbprefix.'category_description cd', 'c.category_id = cd.category_id');
$this->db->where(array('c.status' => 1 ,'c.parent_id' => $parent_id));
$this->db->order_by("sort_order", "asc");
$query = $this->db->get()->result_array();
print_r($query);