Welcome Guest, Not a member yet? Register   Sign In
Model To Controller Problem
#1

[eluser]harpster[/eluser]
Beginners question here - I need to ask for advice after several hours of trying to do what I think should be pretty simple. Maybe my MVC concept is wrong but this is for a user login form that submits to the controller which calls the model for a validate query. After validating the user I want to set the session for the user_id and first_name and I was going to do this in the controller but every way I've tried I get an error, usually Fatal error: Call to a member function result() on a non-object.

I just can't seem to be able to access the data from the query in the controller but I can do it easily in the model. So I wonder what I'm doing wrong? Maybe it's better to process everything in the model but can't you access that query data in the controller too? Thanks for the help... It's driving me nuts and I really need to get a better understanding of this concept.


MODEL: simplified code - this works and I can access the query results here by adding the foreach ($query->result() as $row) code.
Code:
function validate()
    {
        $this->db->where('username', $this->input->post('username'));
        $this->db->where('password', md5($this->input->post('password')));
        $query = $this->db->get('users');
        
                
        if($query->num_rows() > 0)
        {
            return $query->result();

        } else  {
        
            echo "not validated"; exit; //msg for for testing only
        }
    }


[b]CONTROLLER simplified code[b] - get errors

Code:
function validate_user()
    {        
        $this->load->model('users_model');
        $query = $this->users_model->validate();
        
        
       //I'm trying to echo user_id & first_name as a test but get errors. This is where I'll be setting the session.

        foreach ($query->result() as $row)
        {
              echo $row->user_id;
              echo $row->first_name;
        }

         exit;
            
       }
#2

[eluser]JasonS[/eluser]
Code:
if($query->num_rows() > 0)
{
  return $query->result();
} else  {
  echo "not validated"; exit; //msg for for testing only
}

Should be:

Code:
if($query->num_rows() > 0)
{
  return $query;
} else  {
  echo "not validated"; exit; //msg for for testing only
}
#3

[eluser]Josh Holloway[/eluser]
you want to return $query in the model or in the controller do:

Code:
function validate_user()
    {        
        $this->load->model('users_model');
        $query = $this->users_model->validate();
        
        
       //I'm trying to echo user_id & first_name as a test but get errors. This is where I'll be setting the session.

        foreach ($query as $row)
        {
              echo $row->user_id;
              echo $row->first_name;
        }

         exit;
            
       }
#4

[eluser]harpster[/eluser]
Wow that was easy and it worked! - Thanks I thought I was close but that nailed it!

I'm not sure how I would have known that you don't use $query->result() as that works in the model and for views. Would be nice if the manual had a note about that... I'm learning that hard way I guess.

Hey I just wondered... generally speaking, is it more preferred to do any processing with the query data in the model or the controller. Now I want to put the user_id in a session I'm not sure where to do this. Or rephrased, is the model usually just for database queries and returning results which are then data is processed in the controller. Maybe it's a loaded question but I wanted to ask. Thanks.
#5

[eluser]beaufrusetta[/eluser]
All the logic/processing should fall in to the models. Controllers should only be used as routers.
#6

[eluser]harpster[/eluser]
Awesome! Thanks - back to coding - looking much better now.




Theme © iAndrew 2016 - Forum software by © MyBB