Welcome Guest, Not a member yet? Register   Sign In
Codeigniter 2.0 - Call to a member function where() on a non-object
#1

[eluser]Lemar76[/eluser]
Hi

I'm using Codeigniter 2.0.1 with PHP 5.3.2. I'm following through this tutorial: http://net.tutsplus.com/articles/news/co...y-6-login/ and getting the error:

Call to a member function where() on a non-object in /var/www/codeigniter/application/models/pa_user_model.php on line 7

I've searched the forum for a solution and found one here:
http://ellislab.com/forums/viewthread/178114/

But I would like to understand why I've to change my line from:

Code:
$query = $this->pa_user_model->validate();

to:

Code:
$query = pa_user_model::validate();

My controller:

Code:
<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Login extends CI_Controller {
    
    function index()
    {
        $data['main_content'] = 'login_form';
        $this->load->view('includes/template', $data);
    }
    
    function validate_credentials()
    {
        $this->load->model('pa_user_model');
        $query = pa_user_model::validate(); #$this->pa_user_model->validate();
        
        if($query)    //user is validated
        {
            $data = array(
                'user_name' => $this->input->post('username'),
                'is_logged_in'    => TRUE
            );
            
            $this->session->set_userdata($data);
            redirect('site/member_area');
        }
        else
        {
            $this->index();
        }
    }
}

?>

My model:

Code:
class Pa_user_model extends CI_Model {
    
    function validate ()
    {
        $this->db->where('user_name', $this->input->post('username'));
        $this->db->where('password', $this->input->post('password'));
        $query = $this->db->get('pa_user');
        
        
        if($query->num_rows == 1)
        {
            return TRUE;
        }
    }
}
#2

[eluser]toopay[/eluser]
Its weird, your error log say : "Call to a member function where() on a non-object in /var/www/codeigniter/application/models/pa_user_model.php", that means the error is happening at your model file not at controller, but later you say that you can access model, by call the model class directly (in other word, your model works fine).
#3

[eluser]WanWizard[/eluser]
If you call your model statically, $this has no value within the model class context. However, since you call the model from a controller, $this in the model with link back to the controller class, and not the model class. (var_dump($this) will show you the Login object).

This means that $this->db exists in your controller, but $this->db doesn't exist in your model.

Is this everything you have in your model? Any other code (in particular, a magic __get())? CI_Model is CI's own class (in system), not something in application/core, that replaces CI's functionality?

If you have read the entire thread you refer to, you have noticed that calling the model statically didn't solve the problem, the problem was elsewhere...
#4

[eluser]toopay[/eluser]
Correct me if i'm wrong, but thread he refer to is different with his problems. At http://ellislab.com/forums/viewthread/178114/, model is not loaded, so function in it not loaded too. In that thread, the error log say something error in controller statement, not model statement.
#5

[eluser]Lemar76[/eluser]
@toopay
You are right, although the change from the other thread solved the problem, but I don't understand why.

I tried now for testing CI version 1.7.3 and with that version and my app_files it is working with the original method call:

Code:
$query = $this->pa_user_model->validate();

I'm new to CI since yesterday, so I've no idea what has changed between this two versions besides the class names from Model to CI_Model and how I should solve it.
#6

[eluser]Lemar76[/eluser]
Ok now I've located where the error comes from. I've used the tutorial http://taggedzi.com/articles/display/aut...eigniter-2 for eclipse to get the autocompletion feature. That causes the error.

The solution was as postet in the tutorial to save another project in eclipse, make the changes in the model and controller class there and include it to your app_project instead of changing the core of your app_project. (Note for me, I should follow a tutorial 100%, not just 99% Wink )
#7

[eluser]toopay[/eluser]
oh,so simple.




Theme © iAndrew 2016 - Forum software by © MyBB