简体   繁体   English

Codeigniter最佳实践模型

[英]Codeigniter best practice model

What is best practice model for method GetAll()? 方法GetAll()的最佳实践模型是什么? I have problem when i can add parametr: where, group, where group itd. 我可以添加parametr时遇到问题:在哪里,在哪里,在哪组itd。

my propositions: 我的主张:

1) Model: 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;
}

Controller: 控制器:

$articles = $this
    ->articles_m
    ->GetAll()
    ->where('id', 2)
    ->get()
    ->result()
;

2) Classic model: 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();
}

Controller: 控制器:

$articles = $this
     ->articles_m
     ->GetAll(['where' => ['id' => 2]])
     ->result()
;

What is better? 什么是更好的?

option 1 is very elastic. 选项1很有弹性。 I can use all method active records. 我可以使用所有方法的活动记录。 But option 2 i must definet where/order_by etc. Group is when i can grouping "where". 但是选项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;
}

you can mix, add array support etc 您可以混合,添加阵列支持等

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM