-
Notifications
You must be signed in to change notification settings - Fork 1.9k
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
ErrorException Trying to get property 'affected_rows' of non-object #1559
Comments
Ok it was because of validation rules.
|
According to codeigniter4#1559 I write some code which allows to filter validationRules to that elements which are provided in $data. ---- Allows to filter validation rules to only that elements which are in $data passed to update method If you'd like to check only fields passed in $data during update just call: $this->update($your_id, $your_data, true); if there is 5 rules in your $validation_rules and only two of them in $your_data array then only that two elements will be checked in validation process if you pass true as a 3rd arg (if you don't provide it or set to false then update will check every rule as is now). Useful: i.e. you want to only update user nickname or password instead of passing all user attributes.
codeigniter4#1559 - if there is no affected_rows set it is better to return 0 instead of exception :)
It should not throw an exception. It seems that there is still no connection to the database when the
I think no, because if you set rules for a field, these rules must run. If you allow to ignore certain fields, add the
Works nice with Models, API updates, and HTML Forms. Note that if you allow to ignore fields not sent in the Model, these can be ignored if you pass data direct from HTTP requests too... |
You are right as far as we don't have methods which are specified to update only part of fields defined in model. I.e. I've got user panel and I want to allow user to change her/his nickname without passing all her/his data to database . In that case imho is better to update only one field ( in updateUserNickName() method ) instead of reading entire user record, update one field, and pass it to database . My solution in this case has better performance: less rules to check , less (not necessary to get current record) and simpler queries , etc. I know it shouldn't work in that way in insert but I think it should in update if developer specify that he/she know what he/she is doing. |
I like your approach. This way we will not need to use "if_exist" when we are sure which fields should be updated. |
I think the same :) There is a way to do exactly the same with if_exist rule (with some bad hacks) but imho approach showed above is better. |
Looks like the core issue here has already been fixed. |
Not sure why this is happening.
Normal update action in one of the models method:
$this->update($id, ['column' => $value])
and then:
return $this->db->affectedRows()
is causing this exception.The text was updated successfully, but these errors were encountered: