我有一个数据库,有两个表教程和地区教程与分区有一对一的关系。在教程中添加页面我试图获得地区列表。
所以我在TutorialsController中编写了代码
下面的添加方法public function add() {
if ($this->request->is('post')) {
$this->Tutorial->create();
if ($this->Tutorial->save($this->request->data)) {
$this->Session->setFlash(__('The tutorial has been saved.'));
return $this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash(__('The tutorial could not be saved. Please, try again.'));
}
}
$districts = $this->Tutorial->District->find('list');
$this->set(compact('districts'));
}
我确保了
var $uses = array('Tutorial','District');
教程中的关系代码
public $belongsTo = array(
'Districts' => array(
'className' => 'Districts',
'foreignKey' => 'districts_id',
'conditions' => '',
'fields' => '',
'order' => ''
)
);
仍然我遇到错误在行代码中的非对象上调用成员函数find()
$districts = $this->Tutorial->District->find('list');
我使用了var $uses = array('Tutorial','District');
为什么还会出现这个错误?
答案 0 :(得分:3)
删除var $uses = array('Tutorial','District');
(模型关联时不必要)并编辑为 -
public $belongsTo = array(
'District' => array( // remove 's'
'className' => 'Districts',
'foreignKey' => 'districts_id',
'conditions' => '',
'fields' => '',
'order' => ''
)
);