Nieuw lid |
|
Hallo,
Ik ben een model aan het schrijven voor een loginsysteem.
Dit maak ik in codeIgniter, dus met MVC model en OOP.
Nu heb ik een heel raar probleem waar ik helemaal gek van wordt.
Als ik een query wil uitvoeren in mijn model krijg ik de volgende foutmelding:
Fatal error: Call to a member function query() on a non-object in F:\Scripting Envoirment\UsbWebserver\Root\urenPlus.com\Website\application\models\userSystem.php on line 93
Zulke foutmeldingen krijg ik bij welke query dan ook, ook als ik Active Records probeer.
Als ik dezelfde query in mijn Controller uitvoer heb ik dit probleem niet.
Nu heb ik geprobeerd handmatig contact te maken met de database vanuit mijn model maar dit werkt niet.
Werkt niet:
$this->load->model('userSystem', '', TRUE);
---
$config['hostname'] = "localhost";
$config['username'] = "myusername";
$config['password'] = "mypassword";
$config['database'] = "mydatabase";
$config['dbdriver'] = "mysql";
$config['dbprefix'] = "";
$config['pconnect'] = FALSE;
$config['db_debug'] = TRUE;
$this->load->model('userSystem', '', $config);
---
$this->load->database();
$this->load->model('userSystem', '', TRUE); --- $config['hostname'] = "localhost"; $config['username'] = "myusername"; $config['password'] = "mypassword"; $config['database'] = "mydatabase"; $config['dbdriver'] = "mysql"; $config['dbprefix'] = ""; $config['pconnect'] = FALSE; $config['db_debug'] = TRUE; $this->load->model('userSystem', '', $config); --- $this->load->database();
(geprobeerd in de construct van de model).
Nu ga ik hier niet de hele code gooien maar wel een gedeelte:
Controller:
<?php
class login extends Controller {
function index()
{
$this -> load -> view('login');
}
function logMeIn()
{
$this -> load -> model('userSystem', '', TRUE);
if(! $this -> userSystem -> logMeIn($_POST) )
{
$data['postMessage'] = $this -> userSystem -> returnErrors();
}else{
$data['postMessage'] = array('Uw bent succesvol ingelogd');
}
$this -> load -> view('login',$data);
}
}
<?php class login extends Controller { function index() { $this -> load -> view('login'); } function logMeIn() { $this -> load -> model('userSystem', '', TRUE); if(! $this -> userSystem -> logMeIn($_POST) ) { $data['postMessage'] = $this -> userSystem -> returnErrors(); }else{ $data['postMessage'] = array('Uw bent succesvol ingelogd'); } $this -> load -> view('login',$data); } }
Model:
<?php /* Created by Paul du Long - 2009 - pauldulong@gmail.com */
class UserSystem extends Model{
//Testing
var $debug = true;
//Database structure
var $db = array(
'table' => 'users',
'userRow' => 'username',
'passRow' => 'password'
);
var $requiredPostField = array('username','password','userType');
var $minimunLength = 1;
var $allowedSelect = array('klant','programmeur','admin');
//Variables
var $loginLvl;
var $errors = array();
var $data;
var $userData;
var $parentId;
function UserSystem()
{
parent::Model();
}
function logMeIn($data)
{
//Insert Post Data
$this -> data = $data;
//Check required fields
$this -> checkFields();
$this -> checkSelectField();
if(! count($this -> errors) )
{
$this -> setLvl();
$this -> setParent(6);
if( $this -> loadUserData() )
{
$this -> createSessions();
$this -> redirectUser();
}else{
return false;
}
}else{
return false;
}
}
function setParent($id)
{
$this -> parentId = $id;
}
function loadUserData()
{
$sql = "SELECT id FROM ".$this -> db['table']." WHERE ".$this -> db['userRow']."=? AND ".$this -> db['passRow']."=? AND parent = ? ";
$sqlData = array(
$_POST[$this -> requiredPostField[0]],
md5($_POST[$this -> requiredPostField[1]]),
$this -> parentId
);
$query = $this -> db -> query($sql,$sqlData);
if( $query -> num_rows() )
{
$this -> userData = $query -> row_array();
return true;
}else{
$this -> errors[] = 'Geen account gevonden met deze gegevens';
return false;
}
}
<?php /* Created by Paul du Long - 2009 - pauldulong@gmail.com */ class UserSystem extends Model{ //Testing var $debug = true; //Database structure 'table' => 'users', 'userRow' => 'username', 'passRow' => 'password' ); var $requiredPostField = array('username','password','userType'); var $minimunLength = 1; var $allowedSelect = array('klant','programmeur','admin'); //Variables var $loginLvl; var $data; var $userData; var $parentId; function UserSystem() { parent::Model(); } function logMeIn($data) { //Insert Post Data $this -> data = $data; //Check required fields $this -> checkFields(); $this -> checkSelectField(); if(! count($this -> errors) ) { $this -> setLvl(); $this -> setParent(6); if( $this -> loadUserData() ) { $this -> createSessions(); $this -> redirectUser(); }else{ return false; } }else{ return false; } } function setParent($id) { $this -> parentId = $id; } function loadUserData() { $sql = "SELECT id FROM ".$this -> db['table']." WHERE ".$this -> db['userRow']."=? AND ".$this -> db['passRow']."=? AND parent = ? "; $_POST[$this -> requiredPostField[0]], md5($_POST[$this -> requiredPostField[1]]), $this -> parentId ); $query = $this -> db -> query($sql,$sqlData); if( $query -> num_rows() ) { $this -> userData = $query -> row_array(); return true; }else{ $this -> errors[] = 'Geen account gevonden met deze gegevens'; return false; } }
|