Esempio n. 1
0
 /**
  * Save to database
  * @return mixed nextID|bool
  */
 function save()
 {
     if ($this->is_dummy) {
         return;
     }
     $this->save_before();
     $res = false;
     $vfs = $this->fields();
     $low_priority = $this->is_delayed();
     $key = $this->get_key();
     $is_autoincrement = $key && $this->is_key_autoincrement();
     /**
      * Update
      */
     if (!empty($this->id) && !$this->is_allocated()) {
         // modify
         $sql = "UPDATE " . ($low_priority ? 'LOW_PRIORITY ' : '') . $this->config->table . " SET";
         foreach ($vfs as $k => $v) {
             // skip this types of fields
             if ($k != 'id' && $v['type'] != 'virtual' && !isset($v['extra']) && $this->in_working_set($k)) {
                 if (!isset($this->data[$k])) {
                     $this->data[$k] = '';
                 }
                 $fld = $this->format_field_sql($k, $this->data[$k]);
                 $sql .= " {$k} = ";
                 $sql .= $fld;
                 $sql .= ',';
             }
         }
         $sql = substr($sql, 0, -1) . " WHERE id = " . $this->id;
         $res = $this->db->query($sql);
     } else {
         // check for position
         $this->assign_position();
         // new
         $sql = "INSERT " . ($low_priority ? 'DELAYED ' : '') . "INTO " . $this->config->table . " ";
         $sql_part = array(array(), array());
         foreach ($vfs as $k => $v) {
             if (!isset($this->data[$k])) {
                 $this->data[$k] = '';
             }
             $no_skip = true;
             // skip this types of fields
             if (empty($this->data[$k]) && $v['type'] == 'timestamp' || $k == 'id' || $v['type'] == 'virtual' || isset($v['extra'])) {
                 $no_skip = false;
             }
             if ($no_skip) {
                 $sql_part[0][] = $k;
                 $sql_part[1][] = $this->format_field_sql($k, $this->data[$k]);
             }
         }
         $sql .= "(" . implode(',', $sql_part[0]) . ") VALUES ";
         $sql .= "(" . implode(',', $sql_part[1]) . ");";
         $res = $this->db->query($sql);
         if ($this->is_allocated() && $res) {
             $this->container->append($this, $this->get_id());
             $this->_is_allocated = false;
         }
         // update ID
         if ($res) {
             if ($is_autoincrement) {
                 $res = $id = $this->db->sql_nextid();
                 $this->set_id($id);
             }
         }
         $this->_is_new = false;
     }
     $this->save_after($res);
     return $res;
 }