Example #1
0
 public static function put($URI, $data, &$txIds = NULL)
 {
     try {
         $URI = self::serviceName($URI);
         if ($commit = !$txIds) {
             $txIds = array();
         }
         if (!isset(self::$tx[$URI['service']])) {
             self::call('begin', $URI);
             self::$tx[$txIds[] = $URI['service']] = array();
         }
         $method = $data ? isset($data['id']) ? 'update' : 'create' : 'delete';
         $links = self::links($URI['concept']);
         $order = self::$txID++;
         $postpone = array();
         $linkNames = array();
         if ($data) {
             $URI['id'] = isset($data['id']) ? $data['id'] : false;
             foreach ($links as $linkName => $linkTarget) {
                 if (isset($data[$linkName]) && is_array($data[$linkName])) {
                     if (self::isConcept($URI['concept'], $linkName)) {
                         $data[$linkName] = self::put(array('concept' => $linkTarget), $data[$linkName], $txIds);
                     } else {
                         $postpone[$linkTarget] = $data[$linkName];
                         $linkNames[$linkTarget] = $linkName;
                     }
                 }
             }
         } else {
             $URI['id'] = isset($data['id']) ? $data['id'] : $URI['id'];
         }
         $result = Controller::call($method, $URI, $data, false, false, true);
         if (is_array($result) && isset($result['id'])) {
             $URI['id'] = $result['id'];
         }
         $index = count(self::$tx[$URI['service']]) - 1;
         self::$tx[$URI['service']][$index]['order'] = $order;
         self::$tx[$URI['service']][$index]['id'] = $URI['id'];
         if (!isset(self::$tx[$URI['service']][$index]['concept'])) {
             self::$tx[$URI['service']][$index]['concept'] = $URI['concept'];
         }
         foreach ($postpone as $linkTarget => $dt) {
             if (Controller::hasOne($URI['concept'], $linkNames[$linkTarget])) {
                 $dt = array($dt);
             }
             foreach ($dt as $ii => $value) {
                 if (!is_array($value)) {
                     $value = array('id' => $value);
                 }
                 $value[self::links($URI['concept'], $linkNames[$linkTarget])] = $URI['id'];
                 self::put(array('concept' => $linkTarget), $value, $txIds);
             }
         }
         if ($commit) {
             $result = array();
             for ($i = count($txIds) - 1; $i >= 0; $i--) {
                 $currentTx = self::$tx[$txIds[$i]];
                 unset(self::$tx[$txIds[$i]]);
                 if (!self::commit(array('service' => $txIds[$i]), $currentTx)) {
                     self::rollback(array('service' => $txIds[$i]), $currentTx);
                     foreach ($currentTx as $i => $st) {
                         $currentTx[$i]['rollback'] = true;
                     }
                 }
                 $result = array_merge($result, $currentTx);
             }
             self::$txID = 0;
             return $result;
         }
     } catch (Exception $e) {
         if (!self::fallback($e, $URI)) {
             self::closeAll();
         }
         return false;
     }
     return $URI['id'];
 }