-
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: Entity casts do not cast the original data #2441
Comments
Yeah that's an interesting one. Probably need to update
|
Casting is only designed to be on read, not on "write" to the Entity. Doing it on write could cause weird data issues when you try to save it. It is only meant as a convenience feature for working with the data, not as something that defines the data structure that gets saved to the database. If you need to convert to a particular type, you can use a setter method which will be automatically called when you set the value on the class.
In this particular example, what is the issue? It all gets saved correctly. I have an inkling that in more complex situations casting when you're checking if something has changed could be problematic but haven't thought through all of that just yet. |
Thanks for reply, but i don't think this is what i want... |
Remember - PHP is a loosely typed language. It's not Java or something. The example case here should work perfectly fine for you since numbers in strings cast to ints automatically. And, I get that it seems a little silly in this simplistic example. But I have a feeling more complex use-cases (like arrays/json/dates) will cause issues if it's cast on the way into the entity. If you're really concerned that the type is correctly an int within the Entity, you'll just cast it on the way in:
|
Hi guys, I'm new to CodeIgniter but I also stumbled on this issue. I'd like to ask you about it again as I'm not sure if there was a full understanding of the possible problem. I believe I have the same problem but:
I have MySQL table created as: CREATE TABLE items (
id int(11) NOT NULL,
type int(11) NOT NULL,
) and trivial entity that looks like this: class Item extends Entity
{
protected $casts = [
'id' => 'integer',
'type' => 'integer',
];
} Now my PHP code is as follows: $item = $model->find($id);
var_dump($item->type, $item->hasChanged('type'));
$item->type = 1;
var_dump($item->type, $item->hasChanged('type'));
if ($item->hasChanged('type') {
$this->sendTypeNotifications($item->id);
} executing it results in:
and sending notifications to my users. This is not what I expect. That @lonnieezell: I think that maybe there was a misunderstanding between you and @swearl. Could you have another look at this report, please? |
Describe the bug
eg: I load a data from model, type value is 1, if only use this, no problem
but if i set the value again
because the original type value is "1"(string)
CodeIgniter 4 version
rc3
Affected module(s)
CodeIgniter\Entity
Context
The text was updated successfully, but these errors were encountered: