Codeigniter最佳实践模型


Codeigniter best practice model

GetAll((方法的最佳实践模型是什么?当我可以添加parameter: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"的时候。

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;
}

您可以混合、添加阵列支持等