2013-07-02 64 views
1

我无法得到一个为什么我不能加载这个自己的书面模型到我的控制器。型号代码:模型没有在codeigniter中加载

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

错误:

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

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

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。

在此先感谢。

回答

1

的语法应为:

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

哦,天哪,你完全正确!我怎么看这个?!感谢您的意见,我先去喝杯咖啡。 –

+0

没问题..很高兴帮助。很容易错过最小的东西:-) –

+0

@MicKri哦,男人,涂料,我完全错过了。 – usumoio

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 

为了使你的模型加载工作,你必须参考当前加载,然后你在呼唤你有什么要装载入文件的上下文中加载什么。祝你好运。

+0

感谢您的快速评论。我已经尝试过,我已经完成了以下工作: '$ this-> load-> model('storage','idiotmodel'); $ storageDetails = $ idiotmodel-> getStorageByAccount($ userData ['accountID']);' 不幸的是,这也不起作用,我只用另一个变量名得到相同的错误。您可以在不指定目标名称的情况下加载模型,我可以在不同模型的控制器中的其他位置使用它,并且工作正常。 –

+0

@PatrickRombouts你试过'$ this-> load-> model('models/storage','storage');' – usumoio

+0

刚试过了,出现如下错误:'找不到你指定的模型:storage' ,删除模型/部分使我们回到原来的错误。 –