Welcome Guest, Not a member yet? Register   Sign In
Model/Controller: Call to a member function on a non-object
#1

[eluser]convan23[/eluser]
I'm currently creating the part of my app that involves the user handling...login/logout, registration, account control, etc.

I'm working on the register account function, specifically the form validation, and I'm running into a problem where it checks to see if the email entered into the form already exists in the database.

Here's the code that relates to the problem:

Quote:Fatal error: Call to a member function on a non-object in /mnt/gs01/herd02/6813/domains/ci.epiphanyeyc.com/html/system/application/controllers/users.php on line 37

Which refers to:
Code:
function _chekemail($str){
    if($this->user_model->get_user('email',$str)){ return false; } else { return true; }
}
Which is in:
Controller:
Code:
function Users()
{
    parent::Controller();
    $this->load->library('encrypt');
    $this->load->model('user_model','',TRUE);
}

function register()
{
    $data['head_title'] = "Create an Account";
    
    // Validation:
    $this->load->library('validation');
            
    $rules['firstname']    = "trim|required|xss_clean";
    $rules['lastname']    = "trim|required|xss_clean";    
    $rules['email']        = "required|valid_email|_chekemail";    
    $rules['password']    = "required|matches[confirm]|min_length[6]";
    $rules['confirm']    = "required";
    
    $this->validation->set_message('required', '<p class="error"><strong>Required Field:</strong> Please enter your %s</p>');
    $this->validation->set_message('valid_email', '<p class="error"><strong>Invalid Email:</strong> Either your %s is invalid or it is already in use.</p>');
    $this->validation->set_message('min_length', '<p class="error"><strong>Invalid Length:</strong> Your %s must not exceed %s characters</p>');
    $this->validation->set_message('max_length', '<p class="error"><strong>Invalid Length:</strong> Your %s must be within %s characters</p>');
    $this->validation->set_message('matches', '<p class="error"><strong>Passwords Don\'t Match:</strong> Please make sure your passwords are the same in both fields</p>');
    
    $this->validation->set_rules($rules);
    
    $fields['firstname'] = 'first name';
    $fields['lastname'] = 'last name';
    $fields['homephone'] = 'home phone';
    $fields['mobilephone'] = 'mobile phone';
    $fields['streetaddress'] = 'street address';
    $fields['citystatezip'] = 'city, state. zip';
    $fields['email'] = 'email address';    
    $this->validation->set_fields($fields);
    // [END] Validation
    
    if ($this->validation->run() == false) { // If the form doesn't validate
        $this->template->load('template/default','/users/index', $data);
    } else { // If the form does validate, create the account and login to it
        $userdata = $this->input->post();
        $userdata['password'] = $this->encrypt->encode($userdata['password']);            
        
        if($this->user->add_user($userdata)) { // If add user sucessfull, login and redirect
            $this->_login('email',$userdata('email'),$this->input->post($password));
            $this->redirect('users/account');
        }
    }
}    
function _chekemail($str){
    if($this->user_model->get_user('email',$str)){ return false; } else { return true; }
}

Model:
Code:
&lt;?php
    
class User_model extends Model {

    function User_model()
    {
        // Call the Model constructor
         parent::Model();
    }

    function get_user($key,$value)
    {
        $query = $this->db->query("SELECT * FROM users WHERE ".$key."=".$value);
        return $query->result();
    }

    function add_user()
    {
        foreach ($_POST as $key => $value) {
            $this->$key = $value;
        }

        if($this->db->insert('users', $this)) { return true; }
    }

    function edit_user()
    {
        foreach ($_POST as $key => $value) {
            $this->$key = $value;
        }

        $this->db->update('users', $this, array('id', $_POST['id']));
    }

}
?&gt;

Also this is my first CI app so if I'm doing anything in a pointless or not-suggested-way way please let me know.
#2

[eluser]Michael Wales[/eluser]
In your controller:
Code:
$rules['email']        = "required|valid_email|_chekemail";

Should be:
Code:
$rules['email']        = "required|valid_email|callback__chekemail";

All callbacks are prepended with 'callback_'

Since you are making your callback a private method (which is a good idea) you will need to include two underscores.
#3

[eluser]LocoDiego[/eluser]
Hi,
I'm making my first app using this framework, but when I publish them in my hosting server I've got a error:

Quote:Fatal error: Call to a member function on a non-object in /home/lokmford/public_html/clientes/farmacia/system/application/controllers/login.php on line 33

The controller code is:

Code:
function __construct()
    {
        parent::Controller();
        $this->load->model('usuarios_model', '', TRUE);
        $this->load->model('ubigeo_model', '', TRUE);
        $this->load->model('farmacia_model', '', TRUE);
        //$this->load->helper('string');
        //$this->load->library('email');
        //$this->load->library('encrypt');
        $this->load->library('validation');
        //$this->load->scaffolding('usuario');
    }

    function index()
    {
        $data['page_title'] = "Inicio";
        $username = $this->input->post('correo', TRUE);
        $password = $this->input->post('clave', TRUE);

        if (isset($username) && $username != '') { // if they tried to login
            $user_id = $this->site_sentry->login_routine();
        } else {
            $this->load->library('validation');

            $fields['email'] = 'Email Address';
            $fields['nombre'] = 'Name';
            $fields['fono'] = 'Phone';
            
            $this->validation->set_fields($fields);
            $data["msj"] = "";
            // Mandamos data para los combos de ubigeo
            $data["dt_departamentos"] = $this->ubigeo_model->getAllDepartamentos(); // <<<-- Line 33
            $data["dt_provincias"] = $this->ubigeo_model->getAllProvincias(15);
            $data["dt_distritos"] = $this->ubigeo_model->getAllDistritos(230);
            $data["dt_farmacias"] = $this->farmacia_model->getComboFarmacias();
            $this->load->view('login/index', $data);
        }
    }

The model is:

Code:
&lt;?php
class ubigeo_model extends Model {

    function ubigeo_model()
    {
        parent::Model();
    }

    function getAllDepartamentos()
    {
        $this->db->orderby('departamento', 'asc');
        return $this->db->get('departamento');
    }
    
    function getAllProvincias($dpto)
    {
        /*$this->db->orderby('provincia', 'asc');
        $this->db->where('iddepartamento', $dpto);
        return $this->db->get('provincia');*/
        return $this->db->query("SELECT idprovincia as 'codigo', provincia as 'dato' FROM provincia WHERE iddepartamento = " . $dpto . " ORDER BY provincia ASC");
    }
    
    function getAllDistritos($prov)
    {
        /*$this->db->orderby('distrito', 'asc');
        $this->db->where('idprovincia', $prov);
        return $this->db->get('distrito');*/
        return $this->db->query("SELECT iddistrito as 'codigo', distrito as 'dato' FROM distrito WHERE idprovincia = " . $prov . " ORDER BY distrito ASC");
    }

CI Version : 1.5.4
PHP Version: 4.4.7

Hope somebody can help me.
#4

[eluser]tonanbarbarian[/eluser]
I have just read a couple of post whereby some people have found issues with validation and PHP4
The recommendation is to not autoload the validation class, and not to load it in the constructor of a controller.

Also since the issue you are having is with the models I would consider taking all of the loads out of the constructor and putting them into the index method

As a general practice I only load code exactly when I need it and not before
This has the added bonus of making sure that the minimal code is loaded at any time and therefore your app runs faster and uses less memory
#5

[eluser]LocoDiego[/eluser]
Thank you very very very much, I didn't know that
I'm gonna change and fix all my controllers.
Cheers from Peru,
PD
@CI_Team : This framework rulez!




Theme © iAndrew 2016 - Forum software by © MyBB