dtkwt62022 2012-10-01 08:28
浏览 78
已采纳

$ this-> Model-> id在CakePHP中的saveAll之前无效

I have the following code in CakePHP 2:

$this->Order->id = 5;
$this->Order->saveAll(array(
    'Order' => array(
        'person_id' => $this->Session->read('Person.id'),
        'amount' => $total,
        'currency_id' => $code                   
    ),
    'Lineitem' => $lineitems  /* a correctly-formatted array */
));

I would expect this to update the row with the Primary Key of 5 in the Order table and then insert the Lineitem rows with an order_id of 5.

However, all it does is create a new row in Order and then use the new id from the new Order record to create the Listitem rows.

Note: I'm only setting the ID as above for debugging purposes and to easily demonstrate this question. In my final code, I'll be checking to see if there's already a pending order with the current person_id and doing $this->Order->id = $var; if there is and $this->Order->create(); if there isn't.

In other words, sometimes I will want it to INSERT (in which case I will issue $this->Order->create(); ) and sometimes I will want it to UPDATE (in which case I will issue $this->Order->id = $var; ). The test case above should produce an UPDATE but it's producing an INSERT instead.

Any idea what I am doing wrong here?

  • 写回答

2条回答 默认 最新

  • dongxuan2577 2012-10-01 08:52
    关注

    The array you pass to Model->saveAll() doesnt't contain the order's id, so Cake creates a new one. If you wanto to update an existing record, either you set the order id in the passed array, or you retrieve it with a find. The documentation explicitly remarks

    If you want to update a value, rather than create a new one, make sure your are passing the primary key field into the data array

    $order = $this->Order->findById(5);
    // ... modify $order if needed
    $this->Order->saveAll(array('Order' => $order, 'LineItem' => $items));
    

    In your case, you may want to use something like the following to be as concise as possible. Model::saveAssociated() is smart enough to create or update depending on the id, but you must provide suitable input. Model::read($fields, $id) initializes the internal $data: for an existing record all fields will be read from the database, but for a nonexistent id, you'll need to supply the correct data for it to succeed. Assuming an order belongsTo a customer, I supply the customer id if the order doesn't exist

    // set the internal Model::$data['Order']
    $this->Order->read(null, 5);
    // You may want to supply needed information to create
    // a new order if it doesn't exist, like the customer
    if (! $this->Order->exists()) {
        $this->Order->set(array("Customer" => array("id" => $customer_id)));
    }
    $this->Order->set(array('LineItem' => $items));
    $this->Order->saveAssociated();
    

    As a final note, it seems you are implementing a shopping cart. If that's the case, maybe it'd be clearer to use a separate ShoppingCart instead of an Order with a finalized flag.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大