php - What does hasChanged() result in true and the original and new values are the same in CI4? - Stack Overflow

admin2025-05-02  0

I am not sure if I am understanding entities fully.

Here is my code snippet:

        $feature['id'] = 'relation/5466186';
        $entity = new \App\Entities\SknowedWinterSportsEntity();

        $original = $this->SknowedWinterSportsModel->find($feature['id']);        
        $entity->sknowedWinterSports_id = $feature['id'];
    
        echo 'Entity Value: '.$entity->sknowedWinterSports_id.'<br>';
        echo 'Original Value: '.$original->sknowedWinterSports_id.'<br>';

        echo $entity->hasChanged('sknowedWinterSports_id');    

The above results in:

      Entity Value: relation/5466186
      Original Value: relation/5466186

      1

I was expecting that to to ZERO and not 1. Why is that please?

I am not sure if I am understanding entities fully.

Here is my code snippet:

        $feature['id'] = 'relation/5466186';
        $entity = new \App\Entities\SknowedWinterSportsEntity();

        $original = $this->SknowedWinterSportsModel->find($feature['id']);        
        $entity->sknowedWinterSports_id = $feature['id'];
    
        echo 'Entity Value: '.$entity->sknowedWinterSports_id.'<br>';
        echo 'Original Value: '.$original->sknowedWinterSports_id.'<br>';

        echo $entity->hasChanged('sknowedWinterSports_id');    

The above results in:

      Entity Value: relation/5466186
      Original Value: relation/5466186

      1

I was expecting that to to ZERO and not 1. Why is that please?

Share Improve this question asked Jan 2 at 5:49 spreadermanspreaderman 1,0822 gold badges14 silver badges50 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

Doing a quick look into the source code for Code Igniter, I can assume that upon initializing a new entity like you are doing here:

$entity = new \App\Entities\SknowedWinterSportsEntity();

The initial entity likely has blank values. When you go to set them doing:

$entity->sknowedWinterSports_id = $feature['id'];

The value that you have set now differs from the value that it likely started off with (empty array or null probably) therefore,

$entity>hasChanged('sknowedWinterSports_id');

Is working as intended because you are setting it to something else that it didn't start off with. (Likely a blank value or something else if you have a default set by a constructor). It does this because inside of the CodeIgniter source code you have this on the CodeIgniter\Entity class.

    /**
     * Holds original copies of all class vars so we can determine
     * what's actually been changed and not accidentally write
     * nulls where we shouldn't.
     *
     * @var array<string, mixed>
     */
    protected $original = [];

They do this to see what has changed inside of the entity and seems they've provided a helper method inside of that class to allow for you to check for changes to the Entity as well. Meaning that it's working as intended because you changed the values from the original, which is an empty array.

转载请注明原文地址:http://anycun.com/QandA/1746132523a92022.html