Patrick Rombouts
Patrick Rombouts

Reputation: 13

Model not loading in codeigniter

I can't get a hold why I cannot load this own written model into my controller. The Model code:

class Storage extends CI_Model
{

function __construct()
{
    parent::__construct();
    $this->load->database();
}

function getStorageByID( $storageID, $accountID = -1 )
{
    $query = $this->db->select('*')->from('storage')->where('storageID', $storageID);
    if ($accountID != -1)
        $query->where('storageAccountID', $accountID);
    return $this->finishQuery( $query );
}

function getStorageByAccount( $accountID )
{
    $query = $this->db->select('*')->from('storage')->where('storageAccountID', $accountID)->limit( $limit );
    return $this->finishQuery( $query );
}

function finishQuery( $query )
{
    $row = $query->get()->result();  
    return objectToArray($row);
}
}

The code in the controller used to load and execute:

$this->load->model('storage'); // Line 147
$storageDetails = $storage->getStorageByAccount( $userData['accountID'] ); // Line 148

The error:

Message: Undefined variable: storage
Filename: controllers/dashboard.php
Line Number: 148
Fatal error: Call to a member function getStorageByAccount() on a non-object in /home/dev/concept/application/controllers/dashboard.php on line 148

I've tried var_dump'ing the model load command, yet that only returns NULL.

Thanks in advance.

Upvotes: 1

Views: 509

Answers (2)

Michael Krikorev
Michael Krikorev

Reputation: 2156

The syntax should be:

$this->load->model('storage');
$storageDetails = $this->storage->getStorageByAccount( $userData['accountID'] );

Upvotes: 1

usumoio
usumoio

Reputation: 3568

You need to reference the model you are calling and the name of the model as you wish to call it from your controller. In this fashion, you can do the following.

 $this->load->model('storage', 'storage');
 $storageDetails = $storage->getStorageByAccount( $userData['accountID'] ); // Line 148

 // but if you wanted you could use your alias to write as followed
 $this->load->model('storage', 'my_storage');
 $storageDetails = $my_storage->getStorageByAccount( $userData['accountID'] ); // Line 148

In order to make your model loading work you must reference what you are loading and then what you are calling what you have loaded within the context of the file you are loading it into. Good Luck.

Upvotes: 0

Related Questions