0

Getting an:

Fatal error: Call to undefined method UsersController::select() application/models/User.php on line 27

It looks like the UsersController is not sending the select function to the db.class.php which I have inside a library folder.

The db.class.php is being loaded however not in this case.

code inside the UserController.php class is:

class User extends Model
{        

/**
 * Login method
 *
 * @todo: update last_login_time
 * @todo: add hashing
 */
public function user_login() {

    $username = $_POST['data']['User']['username'];
    $password = $_POST['data']['User']['password'];


    $bind = array(
        ":username" => $username,
    );
    $result = $this->select("users", "username = :username", $bind);

    //Check the password returned from the db against the password entered
    if (Bcrypt::checkPassword($password, $result[0]['password']) == true) {
        Session::init();

        Session::set('user_logged_in', true);
        Session::set('user_id', $result[0]['id']);
        Session::set('user_name', $result[0]['username']);
        Session::set('user_permission', $result[0]['permission']);
        Session::set('user_role', $result[0]['role']);

        return true;
    } else {
        return false;
    }
}


/**
 * Log out process, deletes cookie, deletes session
 *
 * @todo implement rememberme cookie
 */
public function logout()
{
    // set the remember-me-cookie to ten years ago (3600sec * 365 days * 10).
    // that's obviously the best practice to kill a cookie via php
    // @see http://stackoverflow.com/a/686166/1114320
    //setcookie('rememberme', false, time() - (3600 * 3650), '/', COOKIE_DOMAIN);

    Session::init();
    // delete the session
    Session::destroy();
}

}

Select Function inside db.class.php

public function select($table, $where="", $bind="", $fields="*") {
    $sql = "SELECT " . $fields . " FROM " . $table;
    if(!empty($where))
        $sql .= " WHERE " . $where;
    $sql .= ";";
    return $this->run($sql, $bind);
}

I think it maybe the reference to $this->select() but I'm learning.

John Conde
  • 217,595
  • 99
  • 455
  • 496
jamper
  • 277
  • 2
  • 5
  • 24

1 Answers1

0

The reason for this error is select(), from what I can tell, is not a method of User, Model, or some other parent in the hierarchy of Model.

Instead it is in db.class.php.

Given the current code, I suggest injecting a DB object into User/Model and then delegate to this object directly.

For example:

class Model {
    public function __construct(DB $db) {
        $this->db = $db;
    }
}

Then, to resolve the error, in User do:

$result = $this->db->select("users", "username = :username", $bind);

Note: This is incomplete. So you'll need to fill in a few blanks. Since you are learning, I suggest reading:

Also, .class.php is an old naming convention used in PHP 4. Do not do this. Instead, follow the PSR-4 naming convention.

Community
  • 1
  • 1
Jason McCreary
  • 71,546
  • 23
  • 135
  • 174