Welcome Guest, Not a member yet? Register   Sign In
Quintuple query?
#1

[eluser]Unknown[/eluser]
Disclaimer: I'm something of a noob.
To practice, I'm building a forum using an MVC pattern.

The posts page loads like this: the controller loads a forum model, specific to each forum, which loads a thread model corresponding to the thread, which loads the list of posts. All of this is done using the $this->db->result('Model_name') function to populate each model's variable fields from its corresponding table in MySQL.

My problem arises because I'd like to have each thread record its number of views, so I have the thread model update a column in my MySQL table with ONE controller call to the Thread_model::up_view_count() function:


Code:
function thread($forum_id, $thread_id, $begin)
    {
        $per_page = 10;
        $this->forum_model->initialize($forum_id);

        $thread_model = $this->forum_model->initialize_thread($thread_id);

        $thread_model->initialize_posts($begin, $per_page);

        $thread_model->up_view_count();

        foreach($thread_model->post_list as $post)
        {
            $data['post'] = $post;
            $this->load->view('forum/post', $data);
        }

    }

Here's the thread_model function:

Code:
function up_view_count()
    {
        $sql = "UPDATE threads SET views = views+1 WHERE thread_id = '{$this->thread_id}'";

        $query = $this->db->query($sql);

    }

The problem: Each page view increments the views column by 5.

I checked MySQL internally by running the query and it works fine (increments the 'views' column by one).

I made sure that this is the only place where this function is called in the entire controller class and no other function or constructor touches the views column of the threads table.

I checked the profiler and it lists one instance of the query being run.

I'm using Firefox 6.0 with no firebug addon.

I'm thinking CI must somehow be loading the models multiple times? Because the function is not inside any loops. But how does that square with the profiler reporting only one query?

Thanks in advance for any help with this problem.




Theme © iAndrew 2016 - Forum software by © MyBB