Exemple #1
0
 protected function getInsertValues(AbstractEntity $entity)
 {
     return array('sid' => $entity->get('sid'), 'status' => $entity->get('status', 'enabled'), 'datetime_start' => $entity->need('datetime_start'), 'datetime_end' => $entity->need('datetime_end'), 'capacity' => $entity->get('capacity'));
 }
 /**
  * Saves (updates or creates) an entity.
  *
  * @param AbstractEntity $entity
  * @throws Exception
  * @return AbstractEntity
  */
 public function save(AbstractEntity $entity)
 {
     $connection = $this->entityTable->getAdapter()->getDriver()->getConnection();
     if (!$connection->inTransaction()) {
         $connection->beginTransaction();
         $transaction = true;
     } else {
         $transaction = false;
     }
     try {
         $id = $entity->getPrimary();
         if ($entity->get($id)) {
             /* Update existing entity */
             /* Determine updated properties */
             $updates = array();
             foreach ($entity->need('updatedProperties') as $property) {
                 $updates[$property] = $entity->get($property);
             }
             if ($updates) {
                 $this->entityTable->update($updates, array($id => $entity->get($id)));
             }
             /* Determine new meta properties */
             foreach ($entity->need('insertedMetaProperties') as $metaProperty) {
                 $this->saveMetaByInsert($id, $entity->get($id), $metaProperty, $entity->needMeta($metaProperty), $entity);
             }
             /* Determine updated meta properties */
             foreach ($entity->need('updatedMetaProperties') as $metaProperty) {
                 $this->saveMetaByUpdate($id, $entity->get($id), $metaProperty, $entity->needMeta($metaProperty), $entity);
             }
             /* Determine removed meta properties */
             foreach ($entity->need('removedMetaProperties') as $metaProperty) {
                 $this->deleteMeta($id, $entity->get($id), $metaProperty, $entity);
             }
             $entity->reset();
             $this->getEventManager()->trigger('save.update', $entity);
         } else {
             /* Insert entity */
             $insertValues = $this->getInsertValues($entity);
             if ($entity->getExtra('n' . $id)) {
                 $insertValues[$id] = $entity->getExtra('n' . $id);
             }
             $this->entityTable->insert($insertValues);
             $eid = $this->entityTable->getLastInsertValue();
             if (!(is_numeric($eid) && $eid > 0)) {
                 throw new RuntimeException('Failed to save entity');
             }
             foreach ($entity->need('meta') as $key => $value) {
                 $this->saveMetaByInsert($id, $eid, $key, $value, $entity);
                 if (!$this->entityMetaTable->getLastInsertValue()) {
                     throw new RuntimeException(sprintf('Failed to save entity meta key "%s"', $key));
                 }
             }
             $entity->add($id, $eid);
             $this->getEventManager()->trigger('save.insert', $entity);
         }
         if ($transaction) {
             $connection->commit();
         }
         $this->getEventManager()->trigger('save', $entity);
         return $entity;
     } catch (Exception $e) {
         if ($transaction) {
             $connection->rollback();
         }
         throw $e;
     }
 }