Search code examples
serializationjoomlajoomla-component

Joomla 3.x load form with serialized data


I'm serializing some data to save them in the database as serialized. Reason is because i dont want to create 30 columns in the database.

I've overriden the save method and they are being saved successfuly as serialized string. Problem is how to fill the form fields upon editing the fields.

               <field
                    name="tickets][price]"
                    type="text"
                    default=""
                    class="span6" />

How should i edit the loadFormData or how to solve this ?

protected function loadFormData()
    {
        $data = JFactory::getApplication()->getUserState(
            'com_buildings.edit.building.data',
            array()
        );

        if (empty($data))
        {
            $data = $this->getItem();

            $data->tickets = unserialize($data->tickets);
        }

        return $data;
    }

Solution

  • Are you aware that serialized data is much harder when it comes to searching? Just wanted to make sure in case you wanted to search for your data at one point (and not only store it).

    Having said that, you should replace the following line:

    $data->tickets = unserialize($data->tickets);
    

    With this:

    if (unserialize($data->tickets) !== FALSE)
        $data->tickets = unserialize($data->tickets);