0

コントローラーでクエリを実行していましたが、モデルで実行する必要があることに気付きました。

これは私がコントローラーに持っているコードです

    $this->db->where('page', 'your-secret-skill');
    $this->data['articles'] = $this->article_m->get();

これは、コア モデルにある get メソッドです。

    public function get($id = NULL, $single = FALSE){

    if ($id != NULL) {
        $filter = $this->_primary_filter;
        $id = $filter($id);
        $this->db->where($this->_primary_key, $id);
        $method = 'row';
    }
    elseif($single == TRUE) {
        $method = 'row';
    }
    else {
        $method = 'result';
    }

    if (!count($this->db->ar_orderby)) {
        $this->db->order_by($this->_order_by);
    }
    return $this->db->get($this->_table_name)->$method();
}

これについてどうすれば記事モデルでクエリを実行し、コントローラーでそれをロードできますか?

4

2 に答える 2

1

Articles モデルで CI_Model を継承する必要があり、その中でクエリを次のように記述できます。

class Articles_m extends CI_Model
{
    function __construct()
    {
          parent::__construct();
          $this->table_name = 'articles';
     }


    public function get()
    {
     $this->db->where('page', 'your-secret-skill');
     $query = $this->db->get($this->table_name);
     return $query->result();
    }

}

そして、コントローラーで:

$this->data['articles'] = $this->article_m->get();
于 2013-09-25T14:02:02.403 に答える