Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

$model->update() and $model->save() updates all rows in a table when a primary key is not used #4607

Closed
plava opened this issue Aug 5, 2014 · 4 comments

Comments

@plava
Copy link

plava commented Aug 5, 2014

Issue:

  1. Create a MySQL table that does not use a primary key
  2. Use $model = ModelClassName::findOne(['nonPK'=>'nonPKnumber']) or $model = ModelClassName::find->where(['nonPK'=>'nonPKnumber'])->one()
  3. Set an attribute ($model->someColumnName = 'newValue')
  4. Call $model->save() or $model->update()
  5. All rows in the table at someColumnName will be updated to newValue

Expected outcome - only selected model should be updated.

@plava plava changed the title $model->update() and $model->save() updates all rows in the database when a primary key is not used $model->update() and $model->save() updates all rows in a table when a primary key is not used Aug 5, 2014
@qiangxue
Copy link
Member

qiangxue commented Aug 5, 2014

What is the SQL statement executed?

@plava
Copy link
Author

plava commented Aug 5, 2014

Is there a way to log the SQL statement executed? All I ran was:
$model = \common\models\PostReview::findOne(['post_id'=>'67709']);
$model->text = 'text';
$model->save();

@qiangxue
Copy link
Member

qiangxue commented Aug 5, 2014

You should override PostReview::primaryKey() to define primary keys. Otherwise you will meet the above problem.

However, from the framework point of view, it's better we throw an exception in this case to avoid updating/deleting all rows. Set for 2.0.1 since the change is not trivial and this is a corner case.

@qiangxue qiangxue added this to the 2.0.1 milestone Aug 5, 2014
@ItsReddi
Copy link
Contributor

argh. same here. now i know what was going on.
btw. i dont think that is an enhancement more a critical bug.

like he wrote...
he loaded an specific database entry.
the core methods should update only the loaded one.
This bug is proposed to be an massive data killer.

@qiangxue qiangxue modified the milestones: 2.0.1, 2.0 RC Aug 13, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants