开发者

cakephp not saving update value to related table, but no errors

Update order function finds all orderlines of current order.

Loops through subtracting quantity ordered from stock level and holding new stock level value in $newstock. All good.

But won't save.

Echoes "success" and the correct value but the database is not updated.

The Order status field does update, with the same code syntax.

It also has the same relationships with the Orderline. (Function called from Orderline Controller)

The table relationships are:

'Product'hasM开发者_C百科any = array('Orderline');

'Order' hasMany = array('Orderline');

'Orderline' belongsTo = array('Order', 'Product');

function updateOrder(){
  $this->data['Order']['id'] = $this->getOrderid();
  $orderproducts = $this->Orderline->find('all',array('conditions' =>     
                           array('Orderline.order_id' => $this->data['Order']['id'])));

foreach($orderproducts as $orderproduct){
  $newstock = $orderproduct['Product']['stock']-$orderproduct['Orderline']['quantity'];
  $this->data['Product']['stock']= $newstock;

  if( $this->Product->saveAll($this->data)){
    echo "success"  ;
  }else{
    echo "not saved";
  }
}

  $this->data['Order']['status']= 1;
  $this->Order->saveall($this->data);

}


I want to add that I was running into silent save fails using saveAll because I hadn't cleared the APP/tmp/cache folder.


Solved.

It had actually been adding new rows to the Product table.

This line was missing from the insert in the foreach loop, reminding it to use the current Product id to know where to insert the new data.

$this->data['Product']['id']= $orderproduct['Product']['id'];

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜