【问题标题】:CakePHP Saving data using "hasMany through" -> Failure Array to String conversionCakePHP 使用“hasMany through”保存数据 -> 数组到字符串转换失败
【发布时间】:2014-01-14 21:01:00
【问题描述】:

我尝试使用“hasMany through”模型通过一个查询保存多个数据。 返回 $this->request->data 的值表明:

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] 尚未保存在数据库中。在我的控制器中,我尝试了 saveAll (yes deep => true)、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.');
        }           
    }


}

请帮帮我……我很绝望……

来自德国的问候

【问题讨论】:

    标签: php cakephp has-many-through


    【解决方案1】:

    数组错了?我认为它应该是这样的:

    'ArticleOrder' => array(
        array('article_id' => 2002, 'quantity' => 99),
        array('article_id' => 3151, 'quantity' => 14),
        /* ... */
    );
    

    检查生成表单或数据结构的代码。

    http://book.cakephp.org/2.0/en/models/saving-your-data.html#model-savemany-array-data-null-array-options-array

    【讨论】:

      【解决方案2】:

      我解决了问题:-)

      这是控制器的代码:

          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.');
              }
          }
      }
      

      我刚刚在for-block中格式化了数组

      谢谢

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-03-22
        • 2023-04-08
        • 2019-12-30
        • 1970-01-01
        • 2016-01-02
        • 1970-01-01
        相关资源
        最近更新 更多