views:

205

answers:

2

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'hasMany = 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);

}

A: 

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'];

merle234
A: 

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

mattalexx