Question

I am trying to access the find() on another model in a controller. I am getting a

Call to a member function find() on a non-object error

PollsController.php (controller):

class PollsController extends AppController{
    public function add(){
        $this->set('appNames', $this->App->find('list'));
    }
}

Poll.php (model):

class Poll extends AppModel{
    public $belongsTo = array ('App');
}

App.php (model):

class App extends AppModel{
    public $hasMany = array('Poll');
}

Note: I have also tried $this->Poll->App->find('list') and still get the same error.

Was it helpful?

Solution

App is reserved keyword. When I change the class to SomethingController.php and Something.php, everything works again.

OTHER TIPS

If you want access another model functions you need load model. There is few ways to do it.

This one is only useful for controllers

$this->loadModel('App');
$this->App->find('first');

This one is good for models and controllers

Read more: CakeAPI: Class ClassRegistry

$appModel = ClassRegistry::init('App');
$appModel->find('first');

This one is good for controllers

Read more: CakeAPI: $uses variable in controller

//in Post Controller
public $use=array('Post','App');

Please load modal in your controller

public $uses = array("Poll");

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top