XF 2.0 Unserialize BLOB in Template

Cupara

Well-known member
I have a value in my table that is BLOB so I need to unserialize it during the foreach loop in the template. Is this possible? If so, how or could you point me to a template to look at?

Thanks
 
If the data is an entity you can set it to be turned into a serialized array in the structure.
 
This is my entity file:
PHP:
public static function getStructure(Structure $structure)
    {
        $structure->table = 'xwow_news';
        $structure->shortName = 'GoblinTimes\xenWoW:News';
        $structure->contentType = '';
        $structure->primaryKey = 'news_id';
        $structure->columns = [
            'news_id' => ['type' => self::UINT, 'autoIncrement' => true],
            'news_type' => ['type' => self::STR],
            'news_char' => ['type' => self::STR],
            'news_date' => ['type' => self::UINT],
            'news_itemid' => ['type' => self::UINT],
            'news_context' => ['type' => self::STR, 'default' => NULL],
            'news_bonuslists' => ['type' => self::JSON_ARRAY, 'default' => '']
        ];
        $structure->behaviors = [

        ];
        $structure->getters = [
            
        ];
        $structure->relations = [
            'Roster' => [
                'entity'     => 'GoblinTimes\xenWoW:Roster',
                'type'         => self::TO_MANY,
                'conditions' => 'char_name',
                'key'         => 'news_char'
            ]
        ];

        return $structure;
    }

Do I need to drop JSON_ARRAY and use SERIALIZE instead?
 
Ok back to what I originally wanted then, do this in the template during the foreach condition. Any takers?
 
If you set the column type properly and are fetching your records via the entity manager then they should be automatically unserialized already before they're even passed to the template.
 
Ok so what should the column type be set to? I have tried JSON, SERIAL and I still get the same result. I am using the entity manager to pull the data.

I'm assuming I need to set it to JSON to match my entity?
 
And what result is that? There is SERIALIZED, SERIALIZED_ARRAY, JSON, and JSON_ARRAY. If you set the type according to the values in the column, encoding and decoding will be taken care of automatically so there is no need to do either one manually.

If that's not working, what value are you getting? You can use {{ dump($entity.column) }} in the template to dump the value. And how are you setting the column value and retrieving the entity for the template to begin with?
 
The value I get when dumping is Array that is all I see. I had entity column set to JSON_ARRAY and database column type set to BLOB which obviously isn't right but then I change it to JSON for the column type and I still see Array instead of the results.

EDIT: So I did the dump in the template and this is all I get for each entry.
PHP:
[]

But I know for 3 entries there is data for the items.
 
Last edited:
Ok so I was going about this all wrong, I just need the column type set to varchar and entity type to str. Got it fixed. Thanks all
 
Top Bottom