public function update($id, $data)
 {
     $hydrator = new ObjectProperty();
     $orderData = $hydrator->extract($data);
     $items = $orderData['item'];
     unset($orderData['item']);
     try {
         $this->repository->beginTransaction();
         $this->repository->update($orderData, $id);
         $listItemsIds = [];
         foreach ($items as $item) {
             if (array_key_exists('id', $item)) {
                 $itemId = $item['id'];
                 $this->repository->updateItem($item, $itemId);
             } else {
                 $item['order_id'] = $id;
                 $itemId = $this->repository->insertItem($item);
             }
             $listItemsIds[] = $itemId;
         }
         $this->repository->deleteItemsNotInArray($id, $listItemsIds);
         $this->repository->commitTransaction();
     } catch (Exception $e) {
         $this->repository->rollbackTransaction();
         return false;
     }
     return $this->repository->find($id);
 }
Exemple #2
0
 public function update($idOrder, $data)
 {
     $order = $this->ordersRepository->find($idOrder);
     $items = $data->items;
     unset($data->items);
     $total = 0;
     $orderTable = $this->ordersRepository->getTableGateway();
     try {
         // Begin Transaction
         $orderTable->getAdapter()->getDriver()->getConnection()->beginTransaction();
         foreach ($items as $key => $item) {
             $item['total'] = $item['quantity'] * $item['price'];
             $items[$key]['total'] = $item['total'];
             $total += $item['total'];
         }
         foreach ($items as $item) {
             if (isset($item['id'])) {
                 $this->ordersRepository->updateItem($item['id'], $item);
             } else {
                 $item['order_id'] = $order->getId();
                 $this->ordersRepository->insertItem($item);
             }
         }
         $data->total = $this->getTotal($order->getId());
         $this->ordersRepository->update($order->getId(), $data);
         // Commit
         $orderTable->getAdapter()->getDriver()->getConnection()->commit();
         return $this->find($order->getId());
     } catch (\Exception $e) {
         // Rollback
         $orderTable->getAdapter()->getDriver()->getConnection()->rollback();
         throw new \Exception('Error processing order.');
     }
 }