Example #1
0
 /**
  *
  */
 public function save(array $i_allowProps = null)
 {
     if (!($field_types = self::get_field_types())) {
         return false;
     }
     $func = $this->is_loaded() ? 'update' : 'insert';
     if ($func == 'insert') {
         if (array_key_exists(self::property_to_field('adduser'), $field_types)) {
             $this->adduser = User::cuid();
         }
         if (array_key_exists(self::property_to_field('adddate'), $field_types)) {
             $this->adddate = 'NOW';
         }
         foreach ($this->get_defaults() as $var => $value) {
             if (!isset($this->{$var}) && isset($value)) {
                 $this->{$var} = $value;
             }
         }
     } else {
         if (array_key_exists(self::property_to_field('chguser'), $field_types)) {
             $this->chguser = User::cuid();
         }
         if (array_key_exists(self::property_to_field('chgdate'), $field_types)) {
             $this->chgdate = 'NOW';
         }
         foreach ($this->get_defaults() as $var => $value) {
             if (!isset($this->{$var}) && isset($value)) {
                 $this->{$var} = $value;
             }
         }
     }
     $assigns = [];
     $manual_binds = [];
     foreach ($field_types as $field => $type) {
         $prop = self::field_to_property($field);
         if ($i_allowProps) {
             if (!in_array($prop, $i_allowProps)) {
                 continue;
             }
         }
         if ($func == 'insert') {
             if (!isset($this->{$prop})) {
                 continue;
             }
         }
         $value = @$this->{$prop};
         switch ($type) {
             case 'datetime':
             case 'date':
                 if (in_array($value, ['NOW', 'NOW()', 'now'])) {
                     $manual_binds[$field] = 'NOW()';
                 } else {
                     if (ctype_digit($value)) {
                         $manual_binds[$field] = "FROM_UNIXTIME('{$value}')";
                     }
                 }
                 break;
             case 'set':
                 if (!$value) {
                     $value = '';
                     $manual_binds[$field] = 'NULL';
                 } else {
                     if (is_array($value)) {
                         $r = [];
                         foreach ($value as $setval) {
                             $r[] = DB::quote($setval);
                         }
                         $value = implode(',', $r);
                         $manual_binds[$field] = $value;
                     }
                 }
                 break;
         }
         $assigns[$field] = $value;
     }
     $opts = ['limit' => 1];
     if ($func == 'update') {
         $id_col = self::get_id_field();
         $opts['where'] = "{$id_col} = :{$id_col}";
         $opts['bind'] = [":{$id_col}" => $this->id];
     }
     $result = self::$func($assigns, $opts, $manual_binds);
     if ($func == 'insert' && $result) {
         $this->id = $result;
         $this->after_create();
     }
     return $result;
 }