示例#1
0
 /**
  * save function.
  * 
  * @access public
  * @return void
  */
 public function save()
 {
     try {
         $db = new DB();
         $set = '';
         $values = array();
         foreach ($this->_fields as $field) {
             if ($set != '') {
                 $set .= ', ';
             }
             $set .= "{$field} = :{$field}";
             $values[":{$field}"] = $this->{$field};
         }
         if (empty($this->id)) {
             $sql = "INSERT INTO " . $this->table . " SET " . $set;
         } else {
             $values[':id'] = $this->id;
             $sql = "UPDATE " . $this->table . " SET " . $set . " WHERE id = :id";
         }
         $db->query($sql, $values);
         if (empty($this->id)) {
             $this->id = $db->insertId();
         }
         $this->saveLanguages();
         return true;
     } catch (PDOException $e) {
         print "Error!: " . $e->getMessage() . "<br/>";
         die;
     }
 }
示例#2
0
 /**
  * Internal method for save current changes.
  *
  * If you need to do some action overwrite the save method instead of this.
  *
  * @return bool
  *
  * @throws \Quaver\Core\QException
  */
 protected function doSave($changes)
 {
     if (empty($changes[$this->primaryKey])) {
         unset($changes[$this->primaryKey]);
     }
     if (empty($changes)) {
         return true;
     }
     try {
         $preSaveResult = $this->dispatch('preSave', $changes);
         if ($preSaveResult === false) {
             return false;
         }
         if ($preSaveResult !== true) {
             $changes = $preSaveResult;
             if (empty($changes)) {
                 return true;
             }
             $set = '';
             $values = array();
             foreach ($changes as $field => $value) {
                 $set .= "`{$field}` = ?,";
                 $values[] = $this->getFieldType($this->fields[$field]['type'])->castToDatabase($value, $field, $this);
             }
             $set = substr($set, 0, -1);
             $db = new DB();
             if (!$this->exists()) {
                 $sql = "INSERT INTO {$this->table} SET {$set}";
             } else {
                 $values[] = $this->id;
                 $sql = "UPDATE {$this->table} SET {$set} WHERE {$this->primaryKey} = ?";
             }
             $db->query($sql, $values);
         }
     } catch (PDOException $e) {
         throw new QException($e->getMessage());
     }
     if (isset($changes[$this->primaryKey])) {
         $this->id = $changes[$this->primaryKey];
     } elseif (!$this->exists()) {
         $this->id = $db->insertId();
         $this->values[$this->primaryKey] = $this->id;
     }
     foreach ($changes as $field => $value) {
         $this->values[$field] = $value;
         $this->originalValues[$field] = $value;
     }
     $this->isNew = false;
     $this->isDeleted = false;
     //To do: ¿replace this with EventDispatcher method?
     foreach ($this->fields as $field => &$def) {
         if (!empty($def['foreignKey'])) {
             if (isset($this->values[$field]->fields)) {
                 foreach ($this->values[$field]->fields as $field2 => &$def2) {
                     if (!empty($def2['foreignKey']) && $def2['foreignKey'] === $def['foreignKey']) {
                         $this->values[$field]->set($field2, $this);
                         break;
                     }
                 }
             }
         }
     }
     $this->dispatch('postSave');
     return true;
 }
示例#3
0
 /**
  * Save object.
  *
  * @return bool
  */
 public function save()
 {
     try {
         $db = new DB();
         $set = '';
         $values = array();
         foreach ($this->_fields as $field) {
             if ($set != '') {
                 $set .= ', ';
             }
             $set .= "`{$field}` = :{$field}";
             $values[":{$field}"] = isset($this->{$field}) ? $this->{$field} : null;
         }
         if (empty($this->id)) {
             $sql = "INSERT INTO {$this->table} SET " . $set;
         } else {
             $values[':id'] = $this->id;
             $sql = "UPDATE {$this->table} SET " . $set . ' WHERE id = :id';
         }
         $db->query($sql, $values);
         if (empty($this->id)) {
             $this->id = $db->insertId();
         }
         return true;
     } catch (PDOException $e) {
         throw new QException($e->getMessage());
     }
 }