模型未加载到codeigniter中

时间:2013-07-02 21:16:09

标签: php codeigniter model controller

我无法理解为什么我无法将自己编写的模型加载到我的控制器中。型号代码:

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);
}
}

控制器中用于加载和执行的代码:

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

错误:

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

我已经尝试了var_dump的模型加载命令,但只返回NULL。

提前致谢。

2 个答案:

答案 0 :(得分:1)

语法应为:

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

答案 1 :(得分:0)

您需要引用要调用的模型以及您希望从控制器中调用的模型名称。通过这种方式,您可以执行以下操作。

 $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

为了使您的模型加载工作,您必须引用正在加载的内容,然后在正在加载它的文件的上下文中调用已加载的内容。祝你好运。