Skip to content

Update model with save() about updated_at timestamps  #6192

@zhuzhichao

Description

@zhuzhichao

When I save an existent model such as $model->save(['timestamps' => false]) in this pull, I notice that the model still update the updated_at .

I find out the reason, after performUpdate judges that it does not need to update the timestamps, the model executes $this->setKeysForSaveQuery($query)->update($dirty), in function 'update()' , the timestamps update again.

// Model.php
    protected function performUpdate(Builder $query, array $options)
    {
        $dirty = $this->getDirty();

        if (count($dirty) > 0)
        {
            // If the updating event returns false, we will cancel the update operation so
            // developers can hook Validation systems into their models and cancel this
            // operation if the model does not pass validation. Otherwise, we update.
            if ($this->fireModelEvent('updating') === false)
            {
                return false;
            }

            // First we need to create a fresh query instance and touch the creation and
            // update timestamp on the model which are maintained by us for developer
            // convenience. Then we will just continue saving the model instances.
            if ($this->timestamps && array_get($options, 'timestamps', true))
            {
                $this->updateTimestamps();
            }

            // Once we have run the update operation, we will fire the "updated" event for
            // this model instance. This will allow developers to hook into these after
            // models are updated, giving them a chance to do any special processing.
            $dirty = $this->getDirty();

            if (count($dirty) > 0)
            {
                $this->setKeysForSaveQuery($query)->update($dirty);

                $this->fireModelEvent('updated', false);
            }
        }

        return true;
    }
//Builder.php 
public function update(array $values)
{
    return $this->query->update($this->addUpdatedAtColumn($values));
}
//......
protected function addUpdatedAtColumn(array $values)
{
    if ( ! $this->model->usesTimestamps()) return $values;

    $column = $this->model->getUpdatedAtColumn();

    return array_add($values, $column, $this->model->freshTimestampString());
}

here is the function usesTimestamps

//Model.php
public function usesTimestamps()
{
    return $this->timestamps;
}

I want to fix it , it may be need to update the model's attribute timestamps equal to array_get($options, 'timestamps', true), I think . But I don't feel it's enough good .

At last, Sorry for my poor English.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions