2011-06-17 18 views
1

我遇到了一個問題,有一天在PagesController被扔以下錯誤:

Fatal error: Call to a member function find() on a non-object in /home/jmccreary/www/thoroughbredsource.com/cakephp/app/app_controller.php on line 20

app_controller.php線20:

$this->current_user = $this->User->find('first', array('recursive' => 0, 
    'conditions' => array('User.id' => $this->logged_in_user_id))); 

在這種情況下,$thisPagesController的一個實例,但是由於某種原因沒有從app_controller.php繼承User模型。

爲了完整起見,這裏的相關代碼:

pages_controller.php

var $name = 'Pages'; 
var $uses = null; 

function beforeFilter() { 
    parent::beforeFilter(); 

    $this->Auth->allow('*'); 
} 

app_controller.php

var $uses = array('User'); 
var $components = array('Session', 'Cookie', 'RequestHandler', 'DebugKit.Toolbar', 'Auth' => array('autoRedirect' => false, 'loginRedirect' => array('controller' => 'users', 'action' => 'dashboard'), 'flashElement' => 'error', 'loginError' => 'The username or password you provided are incorrect.', 'authError' => 'Please log in first.', 'fields' => array('username' => 'email', 'password' => 'passwd'), 'userScope' => array('User.active' => 1))); 
var $helpers = array('Html', 'Form', 'Session'); 

function beforeFilter() { 
    parent::beforeFilter(); 

    // configure Cookie Component 
    // ... 

    $this->logged_in_user_id = $this->Auth->user('id'); 
    if ($this->logged_in_user_id) { 
     // NOTE: the following runs on each request for the logged in user 
     $this->current_user = $this->User->find('first', array('recursive' => 0, 'conditions' => array('User.id' => $this->logged_in_user_id))); 
    } 
} 

我過去的這個錯誤被改變var $uses = null;var $uses = array(); 。請注意,刪除此行完全導致了相同的錯誤。

最後,我不完全瞭解原來的問題或我的解決方案。我希望有更好的解釋或適當的解決方案。順便說一句,運行CakePHP 1.3.10。

回答

3

根據控制器類的源來源,你可以看到http://api.cakephp.org/view_source/controller/

/** 
* An array containing the class names of models this controller uses. 
* 
* Example: `var $uses = array('Product', 'Post', 'Comment');` 
* 
* Can be set to array() to use no models. Can be set to false to 
* use no models and prevent the merging of $uses with AppController 
* 
* @var mixed A single name as a string or a list of names as an array. 
* @access protected 
* @link http://book.cakephp.org/view/961/components-helpers-and-uses 
*/ 
    var $uses = false; 

使用$uses = array(),你告訴PagesController爲「不使用模型和AppController的合併」 $使用」等等有用。您可以在399行開始的源代碼中看到合併。從我所看到的,它將「null」看作「false」,這意味着它不會加載任何模型,包括AppController中的模型。

有關$uses的其他信息,您還可以看到http://book.cakephp.org/view/961/components-helpers-and-uses

+1

+1 IMO,這是一個幾乎令人不安的篡改OOP如何正常工作。 – webbiedave

+1

奇怪的是第427行:'elseif($ this-> uses!== null || $ this-> uses!== false){$ merge [] ='uses'; }'這裏使用'||' - 而不是'&&' - 總是會導致'true' – webbiedave

+0

@webbiedave - 你看不懂它的正確性。它說:如果它是空的或者它是假的。換句話說,它在這種情況下將null看作是假的。就像我說的,如果它是假的,它不會與AppController合併。因此,如果null等於false,則表示它不會合並。 –

0

您正在擴展AppController,然後將uses變量設置爲false/null,它告訴它根本不使用任何模型。

如果您將它設置爲一個空數組(),您將從與AppController中的數組合並的PagesController中獲得一個空數組,並且它將按預期工作。

這是顯然在手冊中記錄。

+0

明白了。 ** Francois Deschenes **已經在上面提供了我接受的答案。 –