Welcome Guest, Not a member yet? Register   Sign In
[solved]Active Records select statement not produced correctly
#1

[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...
Code:
SELECT *
...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
#2

[eluser]daparky[/eluser]
What did you do to fix it?
#3

[eluser]AzizLight[/eluser]
[quote author="daparky" date="1253302015"]What did you do to fix it?[/quote]
For some reason I had this line before the select method:
Code:
if($num !== NULL)
So the select method was never called because $num is NULL by default. So I just removed the line. I know it's pretty stupid lol.




Theme © iAndrew 2016 - Forum software by © MyBB