[eluser]AzizLight[/eluser]
Hi everybody,
I am creating a small blog script. In my post model, I have a get_posts method that uses active records:
Code:
function get_posts($num = NULL)
{
$this->output->enable_profiler(TRUE);
$this->db->select('posts.id, posts.title, posts.body, posts.created_at, posts.updated_at, users.username');
$this->db->from('posts');
$this->db->join('users', 'posts.user_id = users.id', 'left');
$this->db->where('posts.active',1);
$this->db->order_by('created_at', 'desc');
if ($num !== NULL)
$this->db->limit($num);
$query = $this->db->get();
// if the query managed to retrieve data, return the results in an array of objects
if ($query->num_rows() > 0)
return $query->result();
// if the query did not retrieve any data, return an empty array
return array();
}
This works fine except that the select method produces this query...
...instead of that one:
Code:
SELECT posts.id, posts.title, posts.body, posts.created_at, posts.updated_at, users.username
If I rewrite the method this way:
Code:
function get_posts($num = NULL)
{
if ($num === NULL) {
$query = $this->db->query('SELECT posts.id, posts.title, posts.body, posts.created_at, posts.updated_at, users.username
FROM posts
LEFT JOIN users
ON posts.user_id = users.id
WHERE posts.active = 1
ORDER BY created_at DESC
');
} else {
$query = $this->db->query('SELECT posts.id, posts.title, posts.body, posts.created_at, posts.updated_at, users.username
FROM posts
LEFT JOIN users
ON posts.user_id = users.id
WHERE posts.active = 1
ORDER BY created_at DESC
LIMIT ?
', $num);
}
if ($query->num_rows() > 0) {
return $query->result();
}
return array();
}
It works the way it should..
What am I doing wrong? Why does the select method doesn't produce the correct statement?
EDIT: I fixed the problem. I'm an idiot. I am sorry for the useless post