动态下拉菜单和保存cakephp代码不能一起工作



我有三个表"类别"、"课程"one_answers"科目",具有以下关联

  1. 类别有许多课程
  2. 课程属于类别,课程有许多科目
  3. 科目属于课程,科目有很多笔记
  4. 备注属于主题

当我试图保存数据时,它会给出错误'对null调用成员函数create(('。

class SubjectsController extends AppController {
public $uses = array('Category','Course');
public function add(){
pr($this->request->data);die;
if ($this->request->is('post')) {
pr($this->request->data);die;
$this->Subject->create();
if ($this->Subject->save($this->request->data)) {
$this->Flash->success(__('The subject has been saved.'));
return $this->redirect(array('action' => 'index'));
} else {
$this->Flash->error(__('The subject could not be saved. Please, try again.'));
}
}
}
}

它与Ajax无关。它在错误消息中正确地告诉你。您正试图从null对象调用create函数。在这种情况下,$this->Subject

您有类别和课程模型$this->uses,但没有主题。您可以将其添加到该控制器使用的列表中,也可以通过与您已经使用的$this->Course->Subject的现有关联来访问它。

确保检索到的数据是正确的,然后这样做:

$this->Subject->create();
$this->Subject->set(array($this->request->data);
$this->Subject->save();

最新更新