/** * Save the model instance to the database. * * @return bool */ public function save() { if (!static::$not_autoincremented_key) { return parent::save(); } else { if (!$this->dirty()) { return true; } if (static::$timestamps) { $this->timestamp(); } $this->fire_event('saving'); if ($this->exists) { $query = $this->query()->where(static::$key, '=', $this->get_key()); $result = $query->update($this->get_dirty()) === 1; if ($result) { $this->fire_event('updated'); } } else { $result = $this->query()->insert($this->attributes); $this->exists = $result; if ($result) { $this->fire_event('created'); } } // After the model has been "saved", we will set the original attributes to // match the current attributes so the model will not be viewed as being // dirty and subsequent calls won't hit the database. $this->original = $this->attributes; if ($result) { $this->fire_event('saved'); } return $result; } }
/** * Insert a new record for the association. * * If save is successful, the model will be returned, otherwise false. * * @param Model|array $attributes * @return Model|false */ public function insert($attributes) { if ($attributes instanceof Model) { $attributes->set_attribute($this->foreign_key(), $this->base->get_key()); return $attributes->save() ? $attributes : false; } else { $attributes[$this->foreign_key()] = $this->base->get_key(); return $this->model->create($attributes); } }