I've just set up a simple PHP Code Igniter project on a Windows 7 box, IIS 7, Fast CGI, no modules.
when I load up database in one of the function of a Model class by doing such this->load->database()
the thread seem to stop on that particular line. None of the subsequent operations are done.
class Account_model extends Model {
var $userId = '';
var $userName = '';
var $requestToken = '';
var $accessToken = '';
var $enabled = false;
var $startOfDay;
var $endOfDay;
function Account_model() {
parent::Model();
}
function get($userId) {
$this->load->database();
$query = $this->db->get_where('accounts', array('userId' => $userId), 1, 0);
return $query->result();
}
function insert() {
$this->load->database();
// ** stop **
$this->db->insert('accounts', $this); //never gets to this
}
}
If I simply ommit that line altogether, I get a php exception undefined variable $db in model.
The caller controller:
class SignUp extends Controller {
function SignUp() {
parent::Controller();
}
function createUser() {
echo 'processing';
$this->load->model('Account_model');
$this->Account_model->userId = 'asd';
$this->Account_model->userName = 'test_user_pls_delete';
$this->Account_model->enabled = true;
$this->Account_model->startOfDay = time();
$this->Account_model->insert();
echo 'done'; // never gets to this
}
}
I've verified the database configuration is correct (host name, driver, etc), and able to connect to the database server from the machine using MySQL workbrench.
$active_group = "default";
$active_record = TRUE;
$db['default']['hostname'] = "{omitted}";
$db['default']['username'] = "{omitted}";
$db['default']['password'] = "{omitted}";
$db['default']['database'] = "{omitted}";
$db['default']['dbdriver'] = "mysql";
$db['default']['dbprefix'] = "";
$db['default']['pconnect'] = TRUE;
$db['default']['db_debug'] = TRUE;
$db['default']['cache_on'] = FALSE;
$db['default']['cachedir'] = "";
$db['default']['char_set'] = "utf8";
$db['default']['dbcollat'] = "utf8_general_ci";
The database table name: accounts
Any ideas on how to fix this?
Hmmm... here is my model..
class Account_model extends Model {
function Account_model() {
// Call the Model constructor
parent::Model();
$this->db = $this->load->database('default', TRUE);
}
function showTables() {
$query = "show tables";
$rs = $this->db->query($query);
return $rs->result_array();
}
load the database in $db, so $db can process the query.
You can either autoload the database library, by adding 'database' to the library array in the autoload.php file, located:
application/config/autoload.php
Or, load it ad-hoc. In both cases it will make us of your configuration, located at:
application/config/database.php
Can you verify if your configuration is as above?
The following should get your started...
The Controller:
class SignUp extends Controller {
function SignUp(){
parent::Controller();
}
function createUser(){
echo 'processing';
$this->load->model('Account_model');
$result = $this->Account_model->insert($userId = 'asd', $userName = 'test_user', $enabled = 'true', $startOfDay = time());
echo ($result == TRUE) ? 'insert successful' : 'insert failed';
}
}
The Model:
class Account_model extends Model {
function Account_model(){
parent::Model();
$this->db = $this->load->database('default', TRUE);
}
function add_user($userId, $userName, $requestToken = '', $accessToken = '', $enabled = 'false', $startOfDay = '', $endOfDay = '') {
$user = array(
'UserId' => $userId,
'UserName' => $userName,
'requestToken' => $requestToken,
'accessToken' => $accessToken,
'Enabled' => $enabled,
'startOfDay' => $startOfDay,
'endOfDay' => $endOfDay
);
$this->db->insert('accounts', $user);
return ($this->db->affected_rows() > 0) ? TRUE : FALSE;
}
}
}