Inaccuracy? Edit Entity attributes |
It's probably planned that way, but it doesn't seem right.
When the entity is received by the model, a class is created and the setAttributes() method is called (Set raw data array without any mutations) For simple types, everything is fine. But for a date (or another class) there is a difference. When I edit an entity, my attributes apply mutators. Let's say I want to change my date of birth. PHP Code: // protected $dates = [ 'created_at', 'updated_at', 'date_birth']; I think this is incorrect behavior and you need to use the fill () method in the model or some other option. Summary: The _original attributes will not match the changed _attributes, even if the string value is the same
There has been a lot of discussion related to this on the framework PR and related issue, worth checking out: https://github.com/codeigniter4/CodeIgniter4/pull/6284
I see you have prepared a PR. It has not been accepted into the develop branch.
I think, need to compare attributes using casting
Remove the 'date_birth' field from the $dates property.
If you need to receive the Time object for this field, then specify $casts = ['date_birth' => 'datetime'] in the property. I don't know who decided that when setting the value of the field specified in the $dates property, turn the value into an object. (07-30-2022, 10:54 PM)iRedds Wrote: Remove the 'date_birth' field from the $dates property.Thank you. This fixes the issue. Code: $user->hasChanged() boolean true Question: Why use $dates if it only applies to created_at deleted_at updated_at? Now I met one more shortcoming: setAttributes() during find() findAll() cannot set attributes not from $datamap list. For example PHP Code: $this->fullName = $this->firstName . ' ' . $this->lastName What for? To make the attribute available with new User($data) $user->fill($data)
The setAttributes() method is needed in order to set the data as it is. That is, so that mutations are not applied.
If you want a fullname attribute that is essentially a derivative of firstName and lastName , you can add a getter. PHP Code: public function getFullName(): string
Yes, I know how to get fullName.
The problem is to automatically assign a value to it when you find(). I also know about afterFind =) I thought that it would become clearer when initialized in fill or __construct. |
Welcome Guest, Not a member yet? Register Sign In |