Not a bug Potential issue with instantiateEntity on json array parameters

Jake B.

Well-known member
Affected version
2.1.0 Release Candidate 1
I'm loading smilies from the app cache so I don't need to load them again, this only gives me an array, for example:


193203

I'm using $this->app()->instantiateEntity('XF:Smilie', $smilie) to get this into an entity so I can use some additional functions on it more easily, it populates the entity object just fine:


193204

I'm able to reference all of the string/int/bool columns just fine:

Code:
dump([
    $smilie->smilie_id,
    $smilie->title,
    $smilie->smilie_category_id,
    $smilie->display_order,
    $smilie->display_in_editor,
    $smilie->image_url,
    $smilie->image_url_2x,
]);


193205

However, if I try to use sprite_params, for example, I just get an empty array:

Code:
dump($smilie->sprite_params);

193206

Edit: Actually, this may be because the cached array doesn't have sprite_mode defined, looking into that now
 
Last edited:
When instantiating an entity like this, the values given are as they are stored in (retrieved from) the DB. So in a case like this, that would mean JSON encoded, not pre-decoded like this. So essentially, you can't really do this.

Generally speaking, these sorts of caches are not designed for turning things into entities exactly because of potentially issues like this (and that they often miss out certain columns).
 
Edit: Actually, this may be because the cached array doesn't have sprite_mode defined, looking into that now
I had the exact same problem with my cache validator / entity instantiator:

PHP:
    public function getCacheDataForUser($userId)
    {
        if ($userId instanceof \XF\Entity\User)
        {
            $userId = $userId->user_id;
        }
        
        $cache = [];
        
        /** @var \DBTech\Shop\Entity\Purchase[] $entities */
        $entities = $this->findPurchasesForUser($userId)->fetch();
        foreach ($entities as $entity)
        {
            $entityArray = $entity->toArray(false);
            $entityArray['configuration'] = $entity->getValueSourceEncoded('configuration');
            
            $cache[$entity->getIdentifier()] = $entityArray;
        }
        
        return $cache;
    }

$entityArray['configuration'] = $entity->getValueSourceEncoded('configuration'); being the key point here :)
 
Top Bottom