CakePHP:在调用parent::function()时使用父模型?
在CakePHP:在调用parent::function()时使用父模型?,php,cakephp,Php,Cakephp,在catscocontroller中,我调用了一个父函数 parent:index(); 在此函数中,父控制器(animalcontroller)使用其自己的模型动物: public function index() { $this->set('articles', $this->Animal->find('all')); } 因此,当我在CATSCOcontroller中调用parent:index()时,我将得到一个错误,因为CATSCOcontroller将使用其自
catscocontroller
中,我调用了一个父函数
parent:index();
在此函数中,父控制器(animalcontroller
)使用其自己的模型动物
:
public function index() {
$this->set('articles', $this->Animal->find('all'));
}
因此,当我在CATSCOcontroller
中调用parent:index()
时,我将得到一个错误,因为CATSCOcontroller
将使用其自己的模型Cat
而不是父模型Animal
:
Fatal error: Call to a member function find() on a non-object
而不是使用加载模型:loadModel
Controller::loadModel('Article');
我怎样才能解决这个问题?将父模型“绑定”到子模型的最佳方式是什么?$uses
将Animal
放入数组中:
类注册表
或使用类注册表:
public function index() {
$Animal = ClassRegistry::init('Animal');
$this->set('articles', $Animal->find('all'));
}
你是说在
Cat
模型文件中扩展->类动物扩展AppModel吗?@Dezigo?是的。试着导入它:App::import('Controller','Animals')@Dezigo我一开始就这么做了。但它不起作用。
public function index() {
$this->loadModel('Animal');
$this->set('articles', $this->Animal->find('all'));
}
public function index() {
$Animal = ClassRegistry::init('Animal');
$this->set('articles', $Animal->find('all'));
}