问题: 在我的控制器功能中出现以下错误。初始保存工作正常,看起来它在嵌套保存功能上被阻塞了。关于为什么我会收到这个错误的想法?
错误:
致命错误
错误:在非对象上调用成员函数read()
文件:C:\ metroapps \ Orders \ app \ Controller \ OrderLogsController.php
行:121
注意:如果要自定义此错误消息,请创建app \ View \ Errors \ fatal_error.ctp
代码 :
public function quickAdd($notes = null,$order_id =null, $log_type_id = null) {
$this->request->data['OrderLog']['log_type_id'] = $log_type_id;
$this->request->data['OrderLog']['order_id'] = $order_id;
$this->request->data['OrderLog']['notes'] = $notes;
$this->OrderLog->create();
if ($this->OrderLog->save($this->request->data)) {
$this->Session->setFlash(__('The order log has been saved.'));
$this->Order->read(null, $order_id);
//set checkedout to false
$this->Order->set('checked_out', 0);
$this->Order->set('checked_out_by', null);
$this->Order->save();
return $this->redirect(array('controller' => 'orders','action' => 'view',$order_id));
} else {
$this->Session->setFlash(__('The order log could not be saved. Please, try again.'));
}
$orders = $this->OrderLog->Order->find('list');
$logTypes = $this->OrderLog->LogType->find('list');
$this->set(compact('orders', 'logTypes'));
}
答案 0 :(得分:1)
您可能需要修复模型关系并正确访问相关模型。
确保您的Order和OrderLog模型相关(两个方向)
// in Order model public $hasMany = array('OrderLog'); // in OrderLog model public $belongsTo = array('Order');
然后在你的函数中执行
$this->OrderLog->Order-> .. // instead of $this->Order