1

我不明白为什么我不能将这个自己编写的模型加载到我的控制器中。型号代码:

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'ing 模型加载命令,但它只返回 NULL。

提前致谢。

4

2 回答 2

1

语法应该是:

$this->load->model('storage');
$storageDetails = $this->storage->getStorageByAccount($userData['accountID']);
于 2013-07-02T21:41:07.680 回答
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

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

于 2013-07-02T21:20:55.630 回答