繁体   English   中英

Codeigniter最佳实践模型

[英]Codeigniter best practice model

方法GetAll()的最佳实践模型是什么? 我可以添加parametr时遇到问题:在哪里,在哪里,在哪组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;
}

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

暂无
暂无

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

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