I am currently building my own MVC Framework and I have run into an issue that I can't solve when attempting to use 2 methods from a single model. I know that this isn't an issue with the queries or information being return. I am unsure of the proper way that I can call 2 methods when checking to see if a user is logged in... The issue that I face is both methods are calling the DB and it throws a PDO error which I don't know how to get around the issue.. Any guidance would be nice as I have been banging my head over this issue. Thank you!
Fatal error: Uncaught Error: Call to undefined method PDO::Select() in C:\xampp\htdocs\PicWrist\app\models\user.class.php:318 Stack trace: #0 C:\xampp\htdocs\PicWrist\app\controllers\home.php(21): User->getUser('4') #1 C:\xampp\htdocs\PicWrist\app\core\app.php(36): Home->index() #2 C:\xampp\htdocs\PicWrist\app\init.php(47): App->__construct() #3 C:\xampp\htdocs\PicWrist\public\index.php(5): require('C:\\xampp\\htdocs...') #4 {main} thrown in C:\xampp\htdocs\PicWrist\app\models\user.class.php on line 318
//Controller
<?php
/**
* Load the View
*/
class Controller{
public function view($view, $data = []) {
if (file_exists('../app/views/' . $view . '.php')) {
require_once '../app/views/' . $view . '.php';
}
}
public function model($model, $data = []) {
if (file_exists('../app/models/' . $model . '.class.php')) {
require_once '../app/models/' . $model . '.class.php';
return new $model();
} else
return false;
}
}
?>
//Home Controller
<?php
Class Home extends Controller {
public static $user;
public $errors;
public function __construct() {
self::$user = $this->model('user');
}
public function index() {
$userdata = [];
$data = [];
show(self::$user);
$isLoggedin = self::$user->isLoggedin;
show($isLoggedin);
$userdata = self::$user->getUser($_SESSION['user_id']);
show($userdata);
$this->view('home', $data);
}
}
?>
<?php
/**
* Users
*/
class User
{
private $db;
private $errors = '';
public $isLoggedin = False;
public $isAdmin = False;
public function __construct() {
$this->isLoggedIn();
}
public function isLoggedIn() {
if(isset($_SESSION['user_id'])){
$data['user_id'] = $_SESSION['user_id'];
$db = Database::getInstance();
$query = "SELECT * FROM users WHERE user_id = :user_id LIMIT 1";
$results = $db->Select($query, $data);
if (is_array($results)) {
$this->isLoggedIn = True;
}
}
}
public function getUser($id) {
if(isset($id)) {
$data['user_id'] = intval($id);
$db = Database::getInstance();
$query = 'SELECT * FROM users WHERE user_id = :user_id';
$results = $db->Select($query, $data);
if (is_array($results)) {
return $results;
} else {
return False;
}
} else {
return False;
}
}
//DB
<?php
/**
* Database Connection
*/
class Database {
private $dbHost = DB_HOST;
private $dbUser = DB_USER;
private $dbPass = DB_PASS;
private $dbName = DB_NAME;
private $statment;
private static $dbHandler;
private $error;
public function __construct()
{
$conn = 'mysql:host=' . $this->dbHost . ';dbname=' . $this->dbName;
$options = array(
PDO::ATTR_PERSISTENT => true,
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
);
try {
self::$dbHandler = new PDO($conn, $this->dbUser, $this->dbPass, $options);
} catch (PDOException $e) {
self::$error = $e->getMessage();
echo self::$error;
}
}
public static function getInstance() {
if(self::$dbHandler) {
return self::$dbHandler;
}
return $instance = new Self();
}
public function Select($query, $data = array()) {
$statement = self::$dbHandler->prepare($query);
$result = $statement->execute($data);
If($result) {
$data = $statement->fetchAll(PDO::FETCH_OBJ);
if(is_array($data)) {
// show($data);
return $data;
}
}
return False;
}
public function Update($query, $data = array()) {
$statement = self::$dbHandler->prepare($query);
$result = $statement->execute($data);
If($result) {
return True;
}
return False;
}
}
?>