【问题标题】:Model not loading in codeigniter模型未在 codeigniter 中加载
【发布时间】:2013-07-02 21:16:08
【问题描述】:

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

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。

提前致谢。

【问题讨论】:

    标签: php codeigniter model controller


    【解决方案1】:

    语法应该是:

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

    【讨论】:

    • 天哪,你完全正确!我怎么看这个的?!感谢您的意见,我先去喝杯咖啡休息一下。
    • 没问题..很高兴它有帮助。很容易错过最小的事情:-)
    【解决方案2】:

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

     $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
    

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

    【讨论】:

    • 感谢您的快速评论。我已经尝试过了,我已经完成了以下操作:$this->load->model('storage', 'idiotmodel'); $storageDetails = $idiotmodel->getStorageByAccount( $userData['accountID']); 遗憾的是,这也不起作用,我只用另一个变量名得到了同样的错误。您可以在不指定目标名称的情况下加载模型,我在控制器中的其他地方使用不同的模型,效果很好。
    • @PatrickRombouts 你试过$this->load->model('models/storage', 'storage');
    • 刚试了一下,报错:Unable to locate the model you have specified: storage,去掉models/部分,又回到原来的错误了。
    • 这也是负面的。还尝试了使用带有 parent::Model() 的函数 Model 加载它们的旧方法,也没有给出任何结果。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多