Robert Timons
Robert Timons

Reputation: 103

Why do I get this error - Error: Call to a member function find() on a non-object - Cake 2.0

Why do I get this error?

Fatal Error

Error: Call to a member function find() on a non-object
File: /home/mycake/public_html/app/Controller/TasksController.php
Line: 7

I think it has something to do with using CAKE 2.0 but I think the code in my controller might be CAKE 1.3? And I have done a bit of research but I don't know how to change the code to be in CAKE 2.0. Can anyone help?

This is the TasksController.php page

<?php
class TasksController extends AppController {
    public $name = 'tasks';
    public function index() {
        //THIS IS THE LINE 7
        $tasks = $this->Task->find('all');
        $this->set('tasks', $tasks);
    }
}

If you need any more info please ask because I'm not sure how else to make it more relevant to get an answer :)

Upvotes: 0

Views: 591

Answers (3)

Peacekeeper
Peacekeeper

Reputation: 1

You can try the index like this:

class TasksController extends AppController{


    public function index(){

        $this->set('tasks', $this->Task->find('all'));
    }

If you get the Error: Call to a member function find() on a non-object, You can put:

$this->loadModel('Task');

Upvotes: 0

Martin Bean
Martin Bean

Reputation: 39389

If you controller is called TasksController then it will try and instantiate the Task model automatically. You don’t need to manually specify it. The reason CakePHP is throwing an error is because you’ve pluralised the name (models are singular, so Task not Tasks) and are also camel-cased, meaning they start with an uppercase letter (Task not task).

Upvotes: 1

Lionel Chan
Lionel Chan

Reputation: 8069

CakePHP does auto instantiation of corresponding Model if the particular controller's property $uses is not set, so this means you might accidentally setting the property $uses of your AppController to either null or array().

So, check that if you have $uses in your AppController set to empty, or simply, in your TasksController, overwrite the value with public $uses = array('Task');.

Upvotes: 0

Related Questions