-
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
Bug: Premature empty check in Model->update function. #3896
Comments
Btw. I think this will also happen if only values of forbidden fields (in the sense of 'not in the |
So I figured that in my specific case, the However, I think it would still be useful to empty check Would it make sense to exclude such system keys explicitly in the |
I have the same problem. In my case I have a simple table with only 3 text fields and an id for the PK. No date field (createdAt, etc). If I submit my form without any changes, the save() function fail with the error 'You must use the "set" method to update an entry'. If I edit at least one value, it works. |
Try outputting the entity right before calling |
I print_r() the Entity in the log file, and all the keys are there. But if I don't change anything in the form, it fails. In the print_r() I see the attributes are all filled and are all equal to the original values. There's something wrong in the validation. I think it should update all the fields, regardless if they have change. |
Like I mentioned above, I think the operation principle itself is good the way it is. Can you please post the return value of |
$entity->toRawArray prints an array with only one value that is the submit button and is not in the "allowed fields". So it's empty, nothing to update :
But if I edit one value, I have that value in this array and the update works: |
Which version of CI4 are you using? |
4.0.4 |
Okay, this really is the same behaviour then, I just got a little confused re-reading my own issue report 😆 |
The original idea about how this is handled was to perform an insert or update only when data for an entity is changed, so this is basically the correct behavior. Using the Eventually, you can call If you guys have any ideas for improvements, please share. |
@michalsn I want to tell about my implementation of Entity in CI3. Only available fields can be filled in the fill() method. $allowed = array_diff ($this->fields, $this->immutable); That is, it excludes the addition of fields that are not in the database table. |
@iRedds Okay, but is there something that is working wrong when using I believe that entity and model should be independent, so we should be able to recognize if some values have changed in the entity without relying on fields defined in the model. The reason behind it is that you don't have to necessarily use a database with an entity class. |
@iRedds what you mention is actually the second option I thought about to resolve this specific issue. @michalsn I think the PR I just submitted is important regardless of any changes to the
However, as a second point I would think that |
I just took a look into As |
If there is any general problem with an Entity class it would be better to start a new issue, to make the discussion clear. But if you believe it's related to this thread, you can as well continue the conversation here. |
An example in the first post. (#3896 (comment)) |
I think |
@iRedds In that case, I can't bring myself to think of it as a problem. It's desired behavior. But... I think we can talk about an optional Entity property that will define |
Yes. You right. |
@michalsn Apparently we have different views on the framework. |
@iRedds I think michals proposal of a possible additional option to restrict |
@iRedds I never considered different views on anything as an issue - quite opposite. I proposed the idea of how we can achieve what you want. It's up to you now. Please, keep in mind that we have to think about breaking changes and different use cases here. |
@michalsn ok I will call Maybe |
Actually, |
@sfadschm I did not evaluate Michal's proposal. Look at Laravel 5.x. The community believes that it is necessary to make the framework universal so that it can solve any given task with minimal effort. But it seems to me that there are no good universal tools. Michal says entity and model must be independent. But in fact they are already dependent. As for your issue. |
OT: i'm gone |
@includebeer We merged a PR that will make it more clear now.
The model has only built-in support for working with Entities and that's nothing wrong. Requiring to use Entity exclusively with a model would be a bad idea in my opinion. And yes, that's true that the framework is using Entity only with a model, but that doesn't stop you from using it in a different way - it's a really handy class. |
Describe the bug
Not sure if I'm overseeing something here...
In the
Model->update()
function several checks for empty data are implemented.The last one happens here:
CodeIgniter4/system/Model.php
Lines 970 to 974 in d63020b
Later on, field protection is done:
CodeIgniter4/system/Model.php
Line 984 in d63020b
This can be an issue, if we try to update a database row using an entity (
$model->save($entity)
) and no changes have been submitted. In this case, the entity object will be cast to array here:CodeIgniter4/system/Model.php
Line 959 in d63020b
Hereby, unchanged fields are removed by the
Entity->toRawArray()
function. However, some fields will remain in the$data
array (e.g., the primary key column and the 'honeypot').Therefore, the last empty check is not triggered as
$data
is not empty. AfterdoProtectFields
, the$data
array is empty and consequently triggers an error ('You must use the "set" method to update an entry. ').CodeIgniter 4 version
latest develop
Affected module(s)
System\Model
Expected behavior, and steps to reproduce if appropriate
see description
Context
The text was updated successfully, but these errors were encountered: