【发布时间】:2016-08-10 08:27:11
【问题描述】:
GetAll() 方法的最佳实践模型是什么? 当我可以添加参数时我遇到问题:where,group,where group itd。
我的建议:
1) 型号:
public function GetAll()
{
$this->db->select('articles.*');
$this->db->from('articles');
$this->db->join('admins', 'admins.id=articles.admin_id');
$this->db->where('active', 1)
return $this->db;
}
控制器:
$articles = $this
->articles_m
->GetAll()
->where('id', 2)
->get()
->result()
;
2) 经典 型号:
public function GetAll($params = array())
{
$this->db->select('articles.*');
$this->db->from('articles');
$this->db->join('admins', 'admins.id=articles.admin_id');
$this->db->where('active', 1)
if (array_key_exists('where', $params)) {
$this->db->where($params['where']);
}
return $this->db->get();
}
控制器:
$articles = $this
->articles_m
->GetAll(['where' => ['id' => 2]])
->result()
;
什么更好?
选项 1 非常有弹性。我可以使用所有方法活动记录。但是选项 2 我必须定义 where/order_by 等。Group 是我可以将“where”分组的时间。
【问题讨论】:
标签: php codeigniter activerecord model