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

Database driver and ORM Model update() inconsistency #123

Closed
WanWizard opened this issue May 15, 2011 · 6 comments
Closed

Database driver and ORM Model update() inconsistency #123

WanWizard opened this issue May 15, 2011 · 6 comments
Assignees
Labels

Comments

@WanWizard
Copy link
Member

I noticed that the MySQL and MySQLI drivers query() method return affected_rows() after execution, which means

  • a value of -1 when the query had an error
  • a value of 0 when the query was ok but no rows were affected
  • or the number of affected rows.

The PDO driver only returns rowCount(), so no indication if the query executed correctly or not.

Then, the ORM Model update() method uses this return value, and determines that the update was correct if the rowcount returned is greater than zero. Which in turn means that if no rows were affected, Model::save() returns false.

I don't think this is correct.

  • the DB drivers should be made consistent, which means the PDO driver must return -1 in case of a query error
  • the Model::update() method should only return false if the result of the query was -1
@ghost ghost assigned jschreuder May 15, 2011
@jschreuder
Copy link
Contributor

Was away most of today. No time to fully look into this tonight but it sounds logical. On the other hand, the affected row count from Model::update() should always be 1. If it's not something went wrong (Model::update() only updates the current instance, no others)

@WanWizard
Copy link
Member Author

If you (with MySQL) fetch a record using Model::find(1) and then directly save it, affected rows is zero (update went ok, but no rows were updates since all values were the same).

If your controller contains a

if ($model->save())
{
    // success
}
else
{
    // failure
}

Your application will generate an error if the user requests a record, and clicks save without changing anything.

@jschreuder
Copy link
Contributor

Actually, is_changed() is run before save() is executed and when no change is detected it will always return true

@WanWizard
Copy link
Member Author

Issue is in typing. MySQL stores everything as string, so when you retrieve a record, all column values are strings, However, the fields on my forms and the validation routines behind it aren't, so an int column which was retrieved as "10", will be set to 10 (int!) before calling save(), and I assume that is_changed() will then return true, although for MySQL still nothing has changed.

@jschreuder
Copy link
Contributor

I just changed Orm\Query::update() to return true when affected rows is 0. fuel/orm@42e17df6

@WanWizard
Copy link
Member Author

Thanks. This can be closed then.

benharold pushed a commit to benharold/core that referenced this issue Oct 21, 2013
benharold pushed a commit to benharold/core that referenced this issue Oct 21, 2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants