簡體   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