Inaccuracy? Edit Entity attributes - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forumdisplay.php?fid=28) +--- Forum: CodeIgniter 4 Support (https://forum.codeigniter.com/forumdisplay.php?fid=30) +--- Thread: Inaccuracy? Edit Entity attributes (/showthread.php?tid=82612) |
Inaccuracy? Edit Entity attributes - ozornick - 07-29-2022 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 RE: Inaccuracy? Edit Entity attributes - MGatner - 07-30-2022 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 RE: Inaccuracy? Edit Entity attributes - ozornick - 07-30-2022 I see you have prepared a PR. It has not been accepted into the develop branch. I think, need to compare attributes using casting RE: Inaccuracy? Edit Entity attributes - iRedds - 07-30-2022 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. RE: Inaccuracy? Edit Entity attributes - ozornick - 07-30-2022 (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) RE: Inaccuracy? Edit Entity attributes - iRedds - 07-31-2022 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 RE: Inaccuracy? Edit Entity attributes - ozornick - 07-31-2022 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. |