php - db class function not recognised -
getting an:
fatal error: call undefined method userscontroller::select() application/models/user.php on line 27
it looks userscontroller not sending select function db.class.php have inside library folder.
the db.class.php being loaded not in case.
code inside 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 password returned db against 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 remember-me-cookie ten years ago (3600sec * 365 days * 10). // that's best practice kill cookie via php // @see http://stackoverflow.com/a/686166/1114320 //setcookie('rememberme', false, time() - (3600 * 3650), '/', cookie_domain); session::init(); // delete session session::destroy(); } }
select function inside db.class.php
public function select($table, $where="", $bind="", $fields="*") { $sql = "select " . $fields . " " . $table; if(!empty($where)) $sql .= " " . $where; $sql .= ";"; return $this->run($sql, $bind); }
i think maybe reference $this->select() i'm learning.
the reason error select()
, can tell, not method of user
, model
, or other parent in hierarchy of model
.
instead in db.class.php.
given current code, suggest injecting db object user
/model
, delegate object directly.
for example:
class model { public function __construct(db $db) { $this->db = $db; } }
then, resolve error, in user
do:
$result = $this->db->select("users", "username = :username", $bind);
note: incomplete. you'll need fill in few blanks. since learning, suggest reading:
also, .class.php old naming convention used in php 4. not this. instead, follow psr-4 naming convention.
Comments
Post a Comment