I ran into a problem the other day where the PagesController was throwing the following error:
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 line 20:
$this->current_user = $this->User->find('first', array('recursive' => 0,
'conditions' => array('User.id' => $this->logged_in_user_id)));
In this case $this was an instance of PagesController, but for whatever reason had not inherited the User model from app_controller.php.
For completeness, here’s the relevant code:
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)));
}
}
I got past this error by change var $uses = null; to var $uses = array();. Note that removing this line completely resulted in the same error.
In the end, I don’t fully understand the original problem or my solution. I would appreciate a better explanation or what the appropriate solution should be. BTW, running CakePHP 1.3.10.
According to the Controller class’ source source which you can see at http://api.cakephp.org/view_source/controller/:
By using
$uses = array(), you’re telling the PagesController to “use no model and merge with AppController’ $uses” so it works. You can see the merge in the source code starting on line 399. From what I can see, it treats “null” the same as “false” which means that it won’t load any model including the ones from the AppController.You can also see http://book.cakephp.org/view/961/components-helpers-and-uses for additional information on
$uses.