2014-01-14 31 views
0

我嘗試使用'hasMany through'模型通過一個查詢保存多個數據。 返回的這 - $>請求 - >數據中的值示出了這一點:CakePHP使用「hasMany through」保存數據 - > Failure Array to String轉換

Array 
(
[Order] => Array 
    (
     [id] => 
     [customer_id] => 4711 
     [orderDate] => Array 
      (
       [month] => 01 
       [day] => 14 
       [year] => 2014 
      ) 
     ... 
    ) 

[ArticleOrder] => Array 
    (
     [article_id] => Array 
      (
       [0] => 2002 
      ) 

     [quantity] => Array 
      (
       [0] => 99 
      ) 

    ) 

數組[ArticleOrder]還沒有被保存在數據庫中。在我的控制器我試圖白水(是深=>真),saveMany,saveAssociated ...

這裏是控制器:

public function add() { 
    $this->set('customers', $this->Order->Customer->find('list')); //Auflösung der Schlüssel 
    //$this->set('articles', $this->Order->ArticleOrder->Article->find('list')); 
    if(!empty($this->request->data)) { 
     $this->Order->create(); 
     pr($this->data); 
     if($this->Order->ArticleOrder->saveAll($this->request->data, array('deep' => true))) {   
      $this->Session->setFlash("Die Bestellung wurde gespeichert!"); 
      $this->redirect(array('action'=>'index')); 
     } else { 
      $this->Session->setFlash('Die Bestellung konnte nicht gespeichert werden.'); 
     }   
    } 


} 

請幫我...我...絕望

來自德國的問候

回答

0

我解決了這個問題:-)

這裏是控制器的代碼:I剛剛格式化在用於塊

陣列

public function add() { 
    $this->set('customers', $this->Order->Customer->find('list')); //Auflösung der Schlüssel 
    //$this->set('articles', $this->Order->ArticleOrder->Article->find('list')); 
    if(!empty($this->request->data)) { 
     $this->Order->create(); 
     $data = array(
      'Order' => $this->request->data['Order'], 
      'ArticleOrder' => array() 
     ); 
     for($i = 0; $i< count($this->request->data['ArticleOrder']['article_id']); $i++) { 
      $data['ArticleOrder'][] = array(
       'article_id' => $this->request->data['ArticleOrder']['article_id'][$i], 
       'quantity' => $this->request->data['ArticleOrder']['quantity'][$i] 
      ); 
     }   
     if($this->Order->saveAssociated($data, array('deep'=>true))) {   
      $this->Session->setFlash("Die Bestellung wurde gespeichert!"); 
      $this->redirect(array('action'=>'index')); 
     } else { 
      $this->Session->setFlash('Die Bestellung konnte nicht gespeichert werden.'); 
     } 
    } 
} 

謝謝