【问题标题】:Codeigniter best practice modelCodeigniter 最佳实践模型
【发布时间】:2016-08-10 08:27:11
【问题描述】:

GetAll() 方法的最佳实践模型是什么? 当我可以添加参数时我遇到问题: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”分组的时间。

【问题讨论】:

    标签: php codeigniter activerecord model


    【解决方案1】:
    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;
    }
    

    您可以混合、添加数组支持等

    【讨论】:

      猜你喜欢
      • 2011-01-23
      • 1970-01-01
      • 2015-04-16
      • 1970-01-01
      • 1970-01-01
      • 2012-05-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多