Welcome Guest, Not a member yet? Register   Sign In
Inaccuracy? Edit Entity attributes
#1

(This post was last modified: 07-29-2022, 12:47 PM by ozornick.)

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'];
        $user->syncOriginal(); 
        
        
// hasChanged() > boolean false
        // toRawArray() > date_birth => string (10) "2008-02-06"
        $user->firstName $dto->firstName ?? $user->firstName;
        $user->dateBirth $dto->dateBirth ?? $user->dateBirth->format('Y-m-d');
        // ... other fields edit
        
        
// hasChanged() > boolean true
        // toRawArray() > date_birth => CodeIgniter\I18n\Time (6) 2000-10-13 00:00:00+04:00 MSD
        // BUT I did not change the date of birth - the mutator was applied  


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
Simple CI 4 project for beginners codeigniter-expenses ( topic )
Reply
#2

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
Reply
#3

(This post was last modified: 07-30-2022, 09:21 PM by ozornick.)

I see you have prepared a PR. It has not been accepted into the develop branch.
I think, need to compare attributes using casting
Simple CI 4 project for beginners codeigniter-expenses ( topic )
Reply
#4

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.
Reply
#5

(07-30-2022, 10:54 PM)iRedds Wrote: 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.
Thank you. This fixes the issue.

Code:
$user->hasChanged() boolean true
$user->toRawArray(...) array (1)
    date_birth => string (10) "1987-11-06"

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 
Otherwise, a few adjustments are needed for this action in fill() __construct() setAttributes() to set $fullName.
What for? To make the attribute available with new User($data) $user->fill($data)

Simple CI 4 project for beginners codeigniter-expenses ( topic )
Reply
#6

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
{
    return 
$this->firstName ' ' $this->lastName;
}

$user->fullName // return fullname 
Reply
#7

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.
Simple CI 4 project for beginners codeigniter-expenses ( topic )
Reply




Theme © iAndrew 2016 - Forum software by © MyBB