1

In my custom model I want to store the data in serialized form. For achieving it by myself I have done the following thing but instead of all this 0 is getting stored against value

In my class Inchoo_Custombuilders_Model_Mysql4_Cableconnector extends Mage_Core_Model_MySql4_Abstract file

 $this->_serializableFields = array(
        'cable_id' => array(null, array())
    );

I also tried override the _beforeSave Action

 protected function _beforeSave()
 {
    if(is_array($this->getValue())){
        var_dump($this->getValue());die();
        $this->setValue(serialize($this->getValue()));
    }
 }

And I cannot change the database table design because some functionality that is build on the frontend is running from this table only.

What is the proper way to save serialize data into database in Magento.

4
  • Why is your custom functionality in the Inchoo namespace?
    – philwinkle
    Commented Sep 11, 2013 at 14:17
  • I have just written Inchoo here on forum..it's just a example..
    – Pankaj
    Commented Sep 12, 2013 at 4:59
  • what is the fieldtype of the database field?
    – ProxiBlue
    Commented Sep 17, 2013 at 5:15
  • field type is "text"
    – Pankaj
    Commented Sep 17, 2013 at 7:15

2 Answers 2

2

Your methods seem a little bit off. If that _beforeSave() is inside of the Inchoo_Custombuilders_Model_Mysql4_Cableconnector class, then it should have an $object parameter passed in, and you should be checking $object->getValue(), not $this->getValue().

Now, assuming that the field name on the model is value (which by the way I would try to avoid that - I think you might run into mysql reserved keyword issues), and assuming that $object->getValue() contains an array, then what you seem to be doing should basically work fine:

protected function _beforeSave($object)
{
    if ($object->getValue()) {
        $serializedValue =  serialize($object->getValue());
        $this->setValue($serializedValue);
    }
}

If it isn't, then check to see what the value of $serializedValue is to make sure that it is what you expect it to be.

0

Magento supports serialized configuration items already. You can use the standard Serialized Array class as backend model, you only need to define a frontend class with the required columns.

In my example I'll be creating configurable Document Types. Configure your item in config.xml:

<document_types translate="label">
    <label>Document types</label>
    <frontend_model>custombuilders/system_config_form_field_documenttypes</frontend_model>
    <backend_model>adminhtml/system_config_backend_serialized_array</backend_model>
     <sort_order>250</sort_order>
     <show_in_default>1</show_in_default>
     <show_in_website>1</show_in_website>
     <show_in_store>1</show_in_store>
</document_types>

Create your custom frontend model at Inchoo/Custombuilders/Block/System/Config/Form/Field/Documenttypes.php:

  <?php
class Inchoo_Custombuilders_Block_System_Config_Form_Field_Documenttypes extends Mage_Adminhtml_Block_System_Config_Form_Field_Array_Abstract
{
    public function __construct()
    {
        $this->addColumn('value', array(
            'label' => Mage::helper('inchoo_custombuilders')->__('value'),
            'style' => 'width:250px',
        ));
        $this->addColumn('Label', array(
            'label' => Mage::helper('inchoo_custombuilders')->__('Label'),
            'style' => 'width:250px',
        ));
        $this->_addAfter = false;
        $this->_addButtonLabel = Mage::helper('inchoo_custombuilders')->__('Add type');
        parent::__construct();
    }
}

And that's it! You can now add custom document types in admin and retrieve them like so:

$data = Mage::getStoreConfig('custombuilders/view/document_types');
$data = unserialize($data);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.