Framework: Model@update() method updating all rows of table when is new record.

Created on 2 Jul 2015  路  9Comments  路  Source: laravel/framework

To sorry my poor English.

I using Laravel framework in my project and once change the new record

$product = new Product();
$product->fill($attributes)->save();

to

$product = new Product();
$product->update($attributes);

and a big problem did.

All rows of products table updated.

This is my wrong way to make the problem, but the Model@update() to updating all table when new record is dangerous.

Most helpful comment

I'm pretty sure the current behavior won't be changed...

The method specifically checks if the model does not exist and then runs the update of the whole table. So it is definitely intended:

public function update(array $attributes = array())
{
    if ( ! $this->exists)
    {
        return $this->newQuery()->update($attributes);
    }

    return $this->fill($attributes)->save();
}

You are also able to call any query builder method on a model and it will run against the respective table.

All 9 comments

I think Model@update() non-exists record should goes create or raise exceptions better.

I'm pretty sure the current behavior won't be changed...

The method specifically checks if the model does not exist and then runs the update of the whole table. So it is definitely intended:

public function update(array $attributes = array())
{
    if ( ! $this->exists)
    {
        return $this->newQuery()->update($attributes);
    }

    return $this->fill($attributes)->save();
}

You are also able to call any query builder method on a model and it will run against the respective table.

That so sad, I knew the Rails update() method just alias of assign attributes and save (that would be create if record not exists).

The update() method keep literal means, but you made the hidden problem in Laravel.

I'll just notice my team _don't use update() method directly_.

Feel free to override the update method in a base model in your project.

Yes, I can override it, but it is hack, my point is implicit result of update method in Laravel framework.

It's not a hack. It's just a way to change the behavior of the method for your project(s) because I'm pretty sure that this won't be changed in the framework itself...

OK, what's the purpose of update method to updating all table? To punch the gay if he make mistake?

The update is the Model level method, if you wants to update all records, you needs find and update them, or make DB::table('users')->update(['status' => xxx]) instead.

I think the update method make two different things is bad,

  • To call method in the instance object should trigger callback.
  • To call method in the static/class just delegate to Query Builder, should not trigger callback.

If we need this feature, we need implement some like updateColumns method (Ralis way) to do it.

Problem : Update and Insert multi record using laravel 5.4

Was this page helpful?
0 / 5 - 0 ratings

Related issues

progmars picture progmars  路  3Comments

kerbylav picture kerbylav  路  3Comments

SachinAgarwal1337 picture SachinAgarwal1337  路  3Comments

shopblocks picture shopblocks  路  3Comments

Fuzzyma picture Fuzzyma  路  3Comments