【问题标题】:CakePHP Posting ErrorCakePHP 发布错误
【发布时间】:2015-03-30 20:33:31
【问题描述】:

问题: 在我的控制器功能中出现以下错误。初始保存正在工作,看起来它被嵌套保存功能阻塞了。关于我为什么会收到此错误的想法?

错误: 致命错误 错误:在非对象上调用成员函数 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'));
}

【问题讨论】:

  • 可能有两个问题。如果您在模型对象上调用函数,那么您必须在调用它的函数中加载它。如果您正在调用控制器函数,请先尝试为其创建对象,然后使用它来调用该函数

标签: php cakephp


【解决方案1】:

您可能需要修复模型关系并正确访问相关模型。

确保您的 Order 和 OrderLog 模型相关(两个方向)

// 在订单模型中 public $hasMany = array('OrderLog'); // 在 OrderLog 模型中 public $belongsTo = array('Order');

然后在你的函数中做

$this->OrderLog->Order->.. // 而不是 $this->Order

【讨论】:

  • 就是这样。谢谢!!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-10-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-02-06
相关资源
最近更新 更多