CakePHP有很多,也有一个

CakePHP有很多,也有一个,php,cakephp,Php,Cakephp,假设我有下一个型号: Order hasMany OrderLine (OrderLine belongsTo Order) OrderLine hasOne Product (Product belongsTo OrderLine) 我想用多个订单行保存一个新订单,其中每个订单行有一个产品。 例如:用户想要购买“PS3”和“XBOX”,因此我的数据库必须如下所示: orders +----+-------- + | id | user_id | +--------------+ | 1

假设我有下一个型号:

Order hasMany OrderLine   (OrderLine belongsTo Order)
OrderLine hasOne Product  (Product belongsTo 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  |
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]
我可以使用
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);
     }
  }
  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'
        ));
     }
  }
很好用。但我不知道如何保存产品元素

我的表格:

<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>

任何帮助都将不胜感激。

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

我不使用
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);
     }
  }
  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;
    });
});
$(函数(){
var i=0;
$(“#新订单行”)。单击(函数(){
$(
'' +
'' +                                                                                                   
'' +
'' +
“菲尔德”+
'' +
'' + 
'' +
“FieldB”+
'' +
'' + 
''
).fadeIn('slow')。追加('#订单行');
$('.oculto').removeAttr('disabled');
++一,;
});
});

我在表单中的更改:我已将
数据[OrderLine][I]
添加到产品输入中。

您应该能够使用以下内容:

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  |
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]
这意味着您的表单应该如下所示:

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  |
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)
(请参阅)或使用循环在一次调用中保存所有内容:

foreach ($this->request->data as $data)
    $this->Order->saveAll($data)

尝试
$orderLine['product_id']=$this->product->getInsertId()并检查正在保存/创建的
产品
记录。-但是详细说明
也不起作用
,您想在创建订单的同时创建产品吗?我认为产品已经在数据库中,您只需设置正确的ID,但可能您正在尝试做一些不同的事情?产品不在数据库中。事实上,我已经解决了这个问题,但我认为我的解决方案非常“丑陋”。我会发布我的答案。所以我应该改变我的数据库设计?什么是产品线?对不起,是指订单线。不,不是你的db设计。仅说明如何构造要保存在数据库中的阵列。如果您不确定,请在操作中使用
debug($this->request->data)
,查看您的数据结构是否与我的示例匹配。我意识到这是一个糟糕的解决方案。我认为@Yoggi的解决方案更好。