Bankzilla
Bankzilla

Reputation: 2096

Call to undefined method UserModel::addUser()

Other functions from the same model are working fine, I'm only having problem with two of them, which both interact with the database. Works locally.

From the error log. PHP Fatal error: Call to undefined method UserModel::getScreens()

I've searched all through google and can't see the reason behind why these to functions wont run on the server. Login also talks to the database and works okay.

I've tried different naming conventions for the calls $this->UserModel->method() $this->userModel->method() etc. Same with changing the loading model. $this->load->model('UserModel', ' ', TRUE);

user.php (controller)

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

session_start();

class User extends CI_Controller {

    function __construct(){
        parent::__construct();
        $this->load->model('usermodel','',TRUE);
    }

    public function register(){
       ..........
       $addUser = $this->usermodel->addUser($this->input->post());
       ..........
    }

    public function screens($id = FALSE){
        $data = $this->checkStatus('screens');
        //userInfo is coming from the checkStatus function.
        //Have verified with a var_dump($user_id) and it appears
        $user_id = $data['userInfo'][0]['id'];
        $data['screens'] = $this->Usermodel->getScreens($user_id);
        if($data){
            $data['title'] = 'My SCREENs';
            if($id){
                $data['id'] = $id;
                $this->load->template('screenview', $data);
            } else {
                $this->load->template('screens', $data);
            }
        }
    }

    public function login(){
        //This works
        ..........
        $result = $this->usermodel->login($email, $password);
        ..........
    }
}

usermodel.php

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
Class UserModel extends CI_Model{

    function __construct(){
        parent::__construct();
        $this->salt = "_________";

    }

    function getScreens($userID){
    //This doesn't work
        $this -> db -> select('*');
        $this -> db -> from('screens');
        $this -> db -> where('user_id = ' . "'" . $userID . "'");
        $query = $this -> db -> get();

        return $query->result_array();
    }

    function addUser($data){
    //This doesn't work
         $data = array(
           'first_name' => $data['firstname'] ,
           'last_name' => $data['lastname'] ,
           'email' => $data['email'],
           'dob' => $data['dob-day'] . '/' . $data['dob-month'] . '/' . $data['dob-year'],
           'height' => $data['height'],
           'weight' => $data['weight'],
           'password' => sha1($data['password1'] . $this->salt),
           'gender' => $data['gender']
        );
        $added = $this->db->insert('users', $data);
        if($added){
            return true;
        } else {
            return false;
        }
    }

    function login($email, $password){
    //This works
        $this -> db -> select('id, email, password');
        $this -> db -> from('users');
        $this -> db -> where('email = ' . "'" . $email . "'");
        $this -> db -> where('password = ' . "'" . sha1($password . $this->salt) . "'");
        $this -> db -> limit(1);

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

        if($query -> num_rows() == 1){
            return $query->result();
        } else {
            return false;
        }
    }
}

Upvotes: 3

Views: 6941

Answers (2)

Bankzilla
Bankzilla

Reputation: 2096

Problem was with git.

Every time I was making a change, git was recognising the file as userModel.php, instead of usermodel.php so every time the call was being made it would look inside usermodel.php and nothing was there except the login function.

=/

Upvotes: 2

Jacob Kranz
Jacob Kranz

Reputation: 951

You cannot use multiple capital letters in your usermodel model. First letter MUST be capitalized and all others MUST be lowercase.

Try:

Class Usermodel extends CI_Model{

And in your controllers

$this->load->model('Usermodel', '', TRUE);
$this->Usermodel->getScreens()

will post documentation in a second

EDIT:

http://codeigniter.com/user_guide/general/models.html

View rules under "Anatomy of Model". I spent way too much time a while back trying to solve this exact problem when I first started with CI :)

EDIT 2:

Also, I had this problem a long time ago too, but $this->usermodel->whatever() would work on my local machine but NOT on the server. Reason? Apache is a pain when it comes to capital letters. Make SURE you have all your U's capitalized in $this->Usermodel

Upvotes: 2

Related Questions