Example #1
0
 /**
  * Save Entity (will insert or update)
  *
  * @param Entity $entity
  *
  * @return bool status of save
  */
 public function save($entity)
 {
     $query = new Query($entity, false);
     $structure = $this->getEntityStructure($entity);
     if ($entity->_saved) {
         // Update
         $result = $query->update()->set($this->getEntityDataArray($entity))->where(array($structure->primaryColumn->name => $entity->_id))->apply();
         // Update relations
         RelationManager::with($entity)->saveRelations();
         return $result;
     } else {
         // Insert
         $id = $query->insert()->into($structure->tableName)->values($this->getEntityDataArray($entity))->apply();
         if ($id === false) {
             return false;
             // @codeCoverageIgnore
         }
         // Update relations
         RelationManager::with($entity)->saveRelations();
         // Save ID and state
         $entity->{$structure->primaryColumn->propertyName} = $id;
         $entity->_id = $id;
         $entity->_saved = true;
         return true;
     }
 }