2016-08-10 3 views
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などグループを定義する必要があります。私は "どこ"をグループ化することができます。

答えて

0
public function GetAll($id='', $search='', $limit='', $order='') 
{ 
    $this->db->select('A.*'); 
    $this->db->from('articles as A'); 
    $this->db->join('admins as B', 'B.id=A.admin_id'); 
    $this->db->where('A.active', '1'); 
    if($id != ''){ 
    $this->db->where('A.id', $id); 
    } 
    ...... etc 
    return $this->db; 
} 

あなたは、ミックスアレイサポートなど

を追加することができます