2015-03-30 53 views
0

問題: 在我的控制器功能中獲取以下錯誤。初始保存正在工作,它看起來像在嵌套保存功能上被阻塞了。想到爲什麼我會得到這個錯誤?CakePHP過帳錯誤

錯誤: 致命錯誤 錯誤:調用一個非對象上讀() 文件的成員函數:C:\ metroapps \訂單\應用\控制器\ OrderLogsController.php
線: 121

注意:如果您要自定義此錯誤信息,創建應用程序\查看\錯誤\ 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

兩個問題的可能。如果您在模型對象上調用函數,則必須將其加載到您調用它的函數中。如果您正在調用控制器函數,然後嘗試爲該函數創建對象,然後使用它來調用該函數 – 2015-03-30 20:37:15

回答

1

您可能需要修復模型關係並正確訪問相關模型。

確保您的訂單和OrderLog模型是相關的(雙向)

 

// in Order model 
public $hasMany = array('OrderLog'); 

// in OrderLog model 
public $belongsTo = array('Order'); 

,然後在你的函數做

 
$this->OrderLog->Order-> .. 

// instead of $this->Order 
+0

就是這樣。謝謝!! – Joe 2015-03-30 20:46:19