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?
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.