1

假设我有下一个模型:

Order hasMany OrderLine   (OrderLine belongsTo Order)
OrderLine hasOne Product  (Product belongsTo OrderLine)

我想保存一个包含许多 OrderLine 的新订单,其中每个 OrderLine 都有一个产品。例如:一个用户想买一个“PS3”和一个“XBOX”,所以我的数据库必须是这样的:

orders
+----+-------- +
| id | user_id |
+--------------+
| 1  | 10      |

orders_line
+------+----------+------------+
| id   | order_id | product_id |
+-----------------+------------+
| 100  |   1      |   1001     |
| 101  |   1      |   1002     |

products
+-------+------+
| id    | name | ...
+--------------+
| 1001  | PS3  |
| 1002  | BOX  |

我可以保存订单和订单行saveAll

OrdersController.php

  public function add() {
     if ($this->request->isPost()) {
        $this->Order->create();
        $this->request->data['Order']['user_id'] = $this->Auth->user('id');
        $this->Order->saveAll($this->request->data);
     }
  }

这很好用。但我不知道如何保存 Products 元素。

我的表格:

<form ...>
<div id="order-lines">
   <div>
      <input name="data[OrderLine][0][user_id]" type="hidden">
      <div class="span3">
         <label>myfield/label>
         <input name="data[Product][0][myfield]" type="text" id="Product0Myfield">
      </div>
      <div>
         <label>fieldb</label>
         <input name="data[Product][0][fieldb]" type="text" id="Product0Fieldb">
      </div>

   <div>
      <input name="data[OrderLine][1][id]" type="hidden">
      <div>    
         <input name="data[Product][1][id]" type="hidden">
         <label>myfield</label>
         <input name="data[Product][1][myfield]" type="text" id="Product1Myfield">
      </div>

      <div class="span3">
         <label>fieldb</label>
         <input name="data[Product][1][fieldb]" type="text" id="Product1Fieldb">
      </div>
    </div>
</div>
</form>

我试过这个:

foreach ($this->request->data['OrderLine'] as &$orderLine) {
    // create the product and populate it with form data
    $this->Product->create();
    $this->Product->save($this->request->data);
    // trying to update the OrderLine foreign key
    $orderLine['product_id'] = $this->Product->id; // doesn't works 
}

任何帮助都感激不尽。

4

2 回答 2

1

我的解决方案,欢迎任何反馈。

我不使用saveAll OrdersController.php

  public function add() {
     if ($this->request->isPost()) {
        $this->Order->create();
        $this->request->data['Order']['user_id'] = $this->Auth->user('id');
        $this->Order->save($this->request->data);
        $i = 0;
        foreach ($this->request->data['OrderLine'] as &$orderLine) {
           $this->OrderLine->create();
           $this->OrderLine->set('order_id', $this->Order->id);
           $this->Product->create();
           $this->Product->save($this->request->data['OrderLine'][$i]['Product'][$i]);
           $this->OrderLine->set('product_id', $this->Product->id);
           $this->OrderLine->save($this->request->data['OrderLine']);
           $i++;
        }
        $this->redirect(array(
           'controller' => 'sites',
           'action' => 'index'
        ));
     }
  }

而我的表单,嗯,实际上我已经使用 JavaScript 来生成表单:

$(function() {
    var i = 0;
    $('#new-order-line').click(function() {
        $(
        '<div class="row-fluid">' +
            '<div class="span3">' +                                                                                                   
                '<input name="data[OrderLine][' + i + '][user_id]" type="hidden"/>' +
                '<input name="data[OrderLine][' + i + '][product_id]" type="hidden"/>' +
                '<label>FieldA</label>' +
                '<input name="data[OrderLine][' + i + '][Product][' + i + '][field_a]" type="text" id="OrderLine' + i + 'ProductFielda"/>' +
            '</div>' + 
            '<div class="span3">' +
                '<label>FieldB</label>' +
                '<input name="data[OrderLine][' + i + '][Product][' + i + '][field_b]" type="text" id="OrderLine' + i + 'ProductFieldb"/>' +
            '</div>' + 
        '</div>'
        ).fadeIn('slow').appendTo('#order-lines');
        $('.oculto').removeAttr('disabled');
        ++i;
    });
});

我对表单的更改:我已将 添加data[OrderLine][i]到产品输入中。

于 2013-01-18T12:39:38.223 回答
1

你应该能够使用这样的东西:

Array
(
    [0] => array(
        [Order] => Array
        (
            [user_id] => 10
        )

        [OrderLine] => array(
            [0] => array
            (
                [product_id] => 1001
            )
            [1] => array
            (
                [product_id] => 1002
            )
        )
    [1] => array(...)
)

这意味着您的表单应如下所示:

data[0][Order][user_id]
data[0][OrderLine][0][product_id]
data[0][OrderLine][1][product_id]

data[1][Order][user_id]
data[1][OrderLine][0][product_id]
data[1][OrderLine][1][product_id]

等等...

使用选项在一次调用中保存所有内容array('deep' => true)(请参阅http://book.cakephp.org/2.0/en/models/saving-your-data.html#model-savemany-array-data-null-array-options-array)或使用循环:

foreach ($this->request->data as $data)
    $this->Order->saveAll($data)
于 2013-01-18T14:28:12.853 回答