What is best practice model for method GetAll()? I have problem when i can add parametr: where, group, where group itd.
my propositions:
1) Model:
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;
}
Controller:
$articles = $this
->articles_m
->GetAll()
->where('id', 2)
->get()
->result()
;
2) Classic model:
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();
}
Controller:
$articles = $this
->articles_m
->GetAll(['where' => ['id' => 2]])
->result()
;
What is better?
option 1 is very elastic. I can use all method active records. But option 2 i must definet where/order_by etc. Group is when i can grouping "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;
}
you can mix, add array support etc
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.