Welcome Guest, Not a member yet? Register   Sign In
Entity misbehavior after upgrading from 4.1.9 to 4.2.1
#1

(This post was last modified: 07-13-2022, 03:02 PM by stevenmalin.)

Just noticed my application started behaving oddly after upgrading to 4.2.1.  Entity attributes that I know have a value are reporting as not having a value. For example, I have an entity like this:
PHP Code:
<?php

namespace App\Entities;

class 
TestEntity extends \CodeIgniter\Entity\Entity
{
    public $datamap = [
        'test_attr' => 'test_attr'
    ];
    public $attributes = [
        'test_attr' => null
    
];


I know it's not necessary to have a datamap setup for an attribute of the same name but I have my reasons.
Now in my code if I set a value for test_attr, I can get the value of the attribute, but if I check the value of the attribute with empty() or isset(), they return true/false respectively.  For example:
PHP Code:
        $t = new \App\Entities\TestEntity();
        $t->test_attr 'hello';
        if(isset($t->test_attr)) { // eval to false
            ...
        }
        if(empty($t->test_attr)) { // eval to true
            ...
        }
        if($t->test_attr) { // eval to true
            ...
        }
        if(empty($myval $t->test_attr)) { // eval to false
            $f false;
        }
        $myval $t->test_attr;
        if(empty($myval)) { // eval to false
            ...
        
I have a lot of places in my code where I use empty()/isset() to check entity attribute values instead of checking if an attribute is 'truthy'.  I can probably just switch my code to check for truthiness, but as an old C developer I have a tendency to be very specific in my checks  Big Grin

It looks like as of 4.2.0, CodeIgniter\Entity\Entity::__isset was altered to include:
PHP Code:
        if ($this->isMappedDbColumn($key)) {
            return false;
        
For now I can probably just override the __isset method to work the old way, but I suspect this may come up for other users as well.
Reply
#2

The change is for fixing this.
https://forum.codeigniter.com/thread-80835.html
Reply
#3

(07-13-2022, 07:18 PM)kenjis Wrote: The change is for fixing this.
https://forum.codeigniter.com/thread-80835.html

Awesome, thank you!
Reply




Theme © iAndrew 2016 - Forum software by © MyBB