I was doing a query in the controller but it came to my attention that I should do it in the model.
This is the code I have in the controller
$this->db->where('page', 'your-secret-skill');
$this->data['articles'] = $this->article_m->get();
and this is the get method I have in the core model:
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();
}
How would I go about this and do the query in the articles model and loading it with the controller?
You should inherit the CI_Model with the Articles model and in it you can write your query as:
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();
}
}
and then in your controller:
$this->data['articles'] = $this->article_m->get();
Remove this line from the controller first:
$this->db->where('page', 'your-secret-skill');
Instead send your-secret-skill
as a parameter to your model function:
$where = array( 'page', 'your-secret-skill' );
$this->data['articles'] = $this->article_m->get('','',$where);
In your model:
public function get($id = NULL, $single = FALSE, $where = array){
if(isset($where) && is_array($where)){
$this->db->where($where);
}
}