A check if model has anything to update |
After one of CodeIgniter updates I started getting CodeIgniter\Database\Exceptions\DataException ("There is no data to update."). That is caused by the commit b5b6ddfa3fb8 ("Add additional empty checks after field protection.").
My problem is that \CodeIgniter\Entity::hasChanged() doesn't take into account model's $allowedFields. It means that if:
then hasChanged() will return true BUT BaseModel will throw "There is no data to update." Could we have some way of checking if BaseModel: ![]() Example: Code: <?php Code: <?php Code: $fooModel = new \App\Models\FooModel(); Current result: Code: array(2) { Code: CodeIgniter\Database\Exceptions\DataException
$allowedFields has only name, and name is not changed.
So There is no data to update. (04-17-2022, 06:09 PM)kenjis Wrote: $allowedFields has only name, and name is not changed.You misunderstood me. I understand what and why happens. I'm looking for a solution.
> $allowedFields doesn't include one of fields used in Entity
Do you mean `agree` is a database field? If so, and you want to save it, you need to add it in $allowedFields. (04-17-2022, 11:12 PM)kenjis Wrote: > $allowedFields doesn't include one of fields used in Entity No. Database table has only "id" and "name" fields. Remaining POST data ("agree" & "submit") are some extra values that should be filtered out.
Entity class is simply a class that represents a single database row.
You should fill only database fields. Why do you need to fill "agree" & "submit"? (04-17-2022, 11:46 PM)kenjis Wrote: Entity class is simply a class that represents a single database row.I know. (04-17-2022, 11:46 PM)kenjis Wrote: You should fill only database fields.That means I need to manually filter POST data. It means more code, more logic, some code duplication. Model already knows allowed fields and has filtering code. I'd expect to get some helper so I don't have to handle checking for changes on my own. (04-17-2022, 11:46 PM)kenjis Wrote: Why do you need to fill "agree" & "submit"?
Entity represents a single database row.
So I must say you should not fill properties that does not exist in database field. And a form is not a database table. You could write your own classes (Entity and/or Model) that meet your needs. (04-18-2022, 04:31 PM)kenjis Wrote: Entity represents a single database row.CodeIgniter's documentation says something different, please take a look at Using Entity Classes → Entity Usage → Filling Properties Quickly Quote from manual: Quote:when saving through the model, only the fields in $allowedFields will actually be saved to the database, so you can store additional data on your entities without worrying much about stray fields getting saved incorrectly Does it mean documentation isn't accurate there?
I don't agree with the way of the user guide.
I think It is bad practice. I think you should fill only database fields. But the description seems to be correct. In your case, $allowedFields has only name, and name is not changed. So there is no data to update, so stray fields are not saved. |
Welcome Guest, Not a member yet? Register Sign In |