2012-07-13 53 views
4

我是cakephp的新手,並嘗試使用控制檯工具生成一些CRUD操作。除了一張桌子(最大)以外,它工作正常。錯誤:調用成員函數find()在cakephp控制器中的非對象

當嘗試添加了新的元素,它返回:

Error: Call to a member function find() on a non-object File: C:\wamp\www\cakephp\app\Controller\ChantiersController.php
Line: 50

這是該行50及以後:

$programs = $this->Chantier->Program->find('list'); 
    $etats = $this->Chantier->Etat->find('list'); 
    $types = $this->Chantier->Type->find('list'); 
    $champsLibres = $this->Chantier->ChampsLibre->find('list'); 
    $feuillesDeRoutes = $this->Chantier->FeuillesDeRoute->find('list'); 
    $directionsPilotes = $this->Chantier->DirectionsPilote->find('list'); 
    $this->set(compact('programs', 'etats', 'types', 'champsLibres',  
      'feuillesDeRoutes', 'directionsPilotes')); 
+0

請出示的var_dump'輸出( $ this-> Chantier-> Program);' – DaveRandom 2012-07-13 16:25:04

+0

你能否包含第49行以防萬一? – Dave 2012-07-13 17:39:19

回答

14

TLDR /答:

你可以修復關聯或加載模型,並刪除通話(->Chantier部分:

$this->loadModel('Program'); 
$programs = $this->Program->find('list'); 

詳情/解釋:

該錯誤基本上意味着你要調用find()在未在控制器中加載模型。

默認情況下,加載Controller的模型。而且,正如你所做的那樣,你可以使用一個模型通過一個加載的模型。 (如果關聯設置正確)。

例如:

//ChantiersController 
$this->Pizza->find('all'); //ERROR - Pizza model isn't loaded 

要解決這個問題,只是模型加載嘗試使用它之前:

$this->loadModel("Pizza"); 
$this->Pizza->find('all'); //All is good - loaded on the line above 

在你的情況,因爲它看起來像你使用與關聯通過其他模型找到的Chantier模型,很可能兩個模型之間的關聯是不正確的。

+0

謝謝。任何想法爲什麼關聯不正確? – 2012-07-13 19:14:45

+0

沒有看到你的關聯,這是不可能的。可能要開始一個新的問題「模型協會不工作」或類似的東西。 – Dave 2012-07-13 19:16:05

0

如果你不想使用loadModel()函數,那麼你可以定義

var $uses = array('Program'); 

您還可以提及其他的型號名稱也數組中

相關問題