Example #1
0
 /**
  * Create or update object in database
  *
  * @return System\Model\Database
  */
 public function save()
 {
     $this->run_tasks(\System\Model\Callback::BEFORE_SAVE);
     $model = get_model($this);
     if ($this->update_check()) {
         if (isset($model::$attrs['pass'])) {
             foreach ($model::$attrs['pass'] as $attr) {
                 $old_attr = $attr . '_old';
                 if (any($this->__get($old_attr)) && $this->{$attr} != $this->{$old_attr}) {
                     $this->{$attr} = hash_passwd($this->{$attr});
                 }
             }
         }
         $data = $this->get_data_raw();
         if (!$this->is_new() && !$this->is_new_object) {
             \System\Database::simple_update($model::get_table(), $model::get_id_col(), $this->id, $data);
         } else {
             $id = \System\Database::simple_insert($model::get_table(), $data);
             if ($id) {
                 $this->id = $id;
             } else {
                 throw new \System\Error\Database(sprintf('Could not save model "%s".', $model));
             }
         }
     }
     $this->save_relations();
     $this->run_tasks(\System\Model\Callback::AFTER_SAVE);
     return $this;
 }