2
GetAll()メソッドのベストプラクティスモデルは何ですか? 私はparametrを追加できるときに問題があります:where、group、where itd group。Codeigniterベストプラクティスモデル
私の提案:
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私はどこで/ order_byなどグループを定義する必要があります。私は "どこ"をグループ化することができます。