mallix
mallix

Reputation: 1429

Codeigniter MY_Model by Jamie

This is driving me nuts..

I have a fresh installation of CI 2.1.3.
Copied MY_Model from here: https://github.com/jamierumbelow/codeigniter-base-model to application/core.
Autoloaded database library in autoload.php
Configured the database.php inside config folder properly.

Extended the MY_Model class like below:

class User_m extends MY_Model{ 

    public $_table = 'user';
    public $primary_key = 'user_id';

}

And in the default controller:

$this->load->model('user_m', 'user');

$row = $this->user->get(1);
echo $row->email;

This is the simplest implementation to see how the CRUD lib works but I get the following error:

Fatal error: Call to a member function where() on a non-object in MY_Model.php on line 135  

Line 135 of MY_Model.php:

$row = $this->_database->where($this->primary_key, $primary_value)
                        ->get($this->_table)
                        ->{$this->_return_type()}();

Upvotes: 0

Views: 1098

Answers (1)

Alexandru Guzinschi
Alexandru Guzinschi

Reputation: 5726

if (!$this->_db)
{
    $this->_database = $this->load->database();
}

No database object will be returned from $this->load->database();

From CI docs:

/**  
 * Database Loader  
 *  
 * @param    mixed   $params     Database configuration options  
 * @param    bool    $return     Whether to return the database object  
 * @param    bool    $query_builder  Whether to enable Query Builder  
 *                   (overrides the configuration setting)  
 *  
 * @return   void|object|bool    Database object if $return is set to TRUE,  
 *                   FALSE on failure, void in any other case  
 */  
 public function database($params = '', $return = FALSE, $query_builder = NULL) 

Try:

if ( $this->db ) {
    $this->_database = $this->db;
} else {
    $this->_database = $this->load->database('default', true, true);
}

Later edit:
I found a fixed version of that model. Replace your core model with this one

Upvotes: 1

Related Questions