문제

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.

도움이 되었습니까?

해결책

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

다른 팁

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");

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top