views:

51

answers:

3

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

  • Id int(11) primary key auto_increment
  • UserId varchar(255) utf8_general_ci
  • userName varchar(255) utf8_general_ci
  • requestToken varchar(255) utf8_general_ci
  • AccessToken varchar(255) utf8_general_ci
  • Enabled tinyint(1)
  • startOfDay time
  • endOfDay time

Any ideas on how to fix this?

+1  A: 

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.

ariawan
A: 

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?

Anzeo
+1  A: 
  1. You need to load the db in in model or in the auto-config (see Anzeo's post)
  2. Can't access model like $this->Account_model->userId = 'asd'; - need to call functions
  3. You can't insert $this (also unsafe)
  4. Change Enabled field in db to type BOOLEAN (TinyInt is unnecessary)

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;
      }
  }
}
Mitchell McKenna
Thanks Mitchell, as mentioned above, my issue was actually a web server configuration one which is yet to be resolved. Thanks for the explanation though - I find it quite helpful. One thing that concerns me from your implementation is you're using the Model as a data access objects as supposed to a 'domain' model.
ronaldwidha
Thanks for the vote up. A lot of people use arrays in CI over passing objects in the way I've shown above (my company has kind of standardized on it), but to each their own :D
Mitchell McKenna