Example #1
0
 /**
  * Save the model to the database.
  *
  * @return bool
  */
 public function save()
 {
     // If the model does not have any dirty attributes, there is no reason
     // to save it to the database.
     if ($this->exists and count($this->dirty) == 0) {
         return true;
     }
     $model = get_class($this);
     // Since the model was instantiated using "new", a query instance has not been set.
     // Only models being used for querying have their query instances set by default.
     $this->query = DB::connection(static::$connection)->table(static::table($model));
     if (property_exists($model, 'timestamps') and $model::$timestamps) {
         $this->timestamp();
     }
     // If the model already exists in the database, we will just update it.
     // Otherwise, we will insert the model and set the ID attribute.
     if ($this->exists) {
         $success = $this->query->where_id($this->attributes['id'])->update($this->dirty) === 1;
     } else {
         $success = is_numeric($this->attributes['id'] = $this->query->insert_get_id($this->attributes));
     }
     $this->exists = true and $this->dirty = array();
     return $success;
 }