> It looks like your error mess sage will only show if the save on the > *last* order_detail fails, since $result is reset with each iteration > of the loop. This action probably should use transaction processing > (commit/rollback support).
it was just off-the-cuff code, merely meant to give an idea. Personally I would assume the data would be written ok, which it should provided it�s validated properly and the DB server is active, so would usually just add a log entry if the save fails. Either that or break out of the loop at the point of failure, deleting the previously created records and showing the form again. > Also, instead of setting the id field to null in each iteration, I > think it's prefered to use "create" before each save: > > $this->Order->Orderdetail->create(); > > Probably does the same thing, but since it's there, it must be the > better way...I hear these cake guys are smart and they wouldn't put it > in there if there was no good reason :) pretty sure it doesn�t make any difference, horses for courses! cheers, jb -- jon bennett t: +44 (0) 1225 341 039 w: http://www.jben.net/ iChat (AIM): jbendotnet Skype: jon-bennett On 4/27/07, NOSLOW <[EMAIL PROTECTED]> wrote: > > It looks like your error message will only show if the save on the > *last* order_detail fails, since $result is reset with each iteration > of the loop. This action probably should use transaction processing > (commit/rollback support). > > Also, instead of setting the id field to null in each iteration, I > think it's prefered to use "create" before each save: > > $this->Order->Orderdetail->create(); > > Probably does the same thing, but since it's there, it must be the > better way...I hear these cake guys are smart and they wouldn't put it > in there if there was no good reason :) > > > > On Apr 27, 2:29 am, "Jon Bennett" <[EMAIL PROTECTED]> wrote: > > sorry, it�s very early here, I meant: > > > > if($this->Order->save($this->data)) > > { > > $order_id = $this->Order->getLastInsertId(); > > > > foreach ($this->data[�OrderDetail�] as $order_detail) > > { > > $data = array(); > > $data[�OrderDetail�][�d�] = null; > > $data[�OrderDetail�][�order_id �] = $order_id; > > $data[�OrderDetail�][�product_id �] = > > $order_detail[�OrderDetail�][�product_id�]; > > $data[�OrderDetail�][�qty �] = > $order_detail[�OrderDetail�][�qty�]; > > $result = $this->Order->Orderdetail->save($data); > > } > > > > if ($result) > > { > > $this->Session->setFlash('The Order has been saved > '.$order_id); > > } > > else > > { > > $this->Session->setFlash('Order details could not be saved > for order > > id: '.$order_id); > > } > > > > } > > > > jon > > > > On 4/27/07, Jon Bennett <[EMAIL PROTECTED]> wrote: > > > > > > > > > hasMany�s are not saved automagically, try: > > > > > if($this->Order->save($this->data)) > > > { > > > $order_id = $this->Order->getLastInsertId(); > > > > > foreach ($this->data[�OrderDetail�] as $order_detail) > > > { > > > $data = array(); > > > $data[�OrderDetail�][�d�] = null; > > > $data[�OrderDetail�][�order_id �] = $order_id; > > > $data[�OrderDetail�][�product_id �] = $order_id; > > > $data[�OrderDetail�][�qty �] = 20; > > > $result = $this->Order->Orderdetail->save($data); > > > } > > > > > if ($result) > > > { > > > $this->Session->setFlash('The Order has been saved > '.$order_id); > > > } > > > else > > > { > > > $this->Session->setFlash('Order details could not be saved > for order id: > > > '.$order_id); > > > } > > > } > > > > > hth > > > > > jon > > > > -- > > > > jon bennett > > t: +44 (0) 1225 341 039 w:http://www.jben.net/ > > iChat (AIM): jbendotnet Skype: jon-bennett > > > > > -- jon bennett t: +44 (0) 1225 341 039 w: http://www.jben.net/ iChat (AIM): jbendotnet Skype: jon-bennett --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Cake PHP" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/cake-php?hl=en -~----------~----~----~----~------~----~------~--~---
