Reputation:
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.
Upvotes: 0
Views: 57
Reputation: 184
Please load modal in your controller
public $uses = array("Poll");
Upvotes: 0
Reputation:
App is reserved keyword. When I change the class to SomethingController.php and Something.php, everything works again.
Upvotes: 1
Reputation: 1263
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');
Upvotes: 0