One day, I needed to create a custom object on Magento, I had a lot of problems, but the most annoying was to create en array attribute in a custom class which inherit Varien_Object !

It looks like a simple thing, but it’s complicated to compute it.

Let’s have a look to Magento’s behaviour when we add an array attribute on our class :

class MiniMax_MyModule_Model_MyObject extends Varien_Object
{
	protected $_myarray = array();

	public function getMyArray(){ $this->myarray;}

	public function setMyArray($value){
		$this->myarray[] = $value;
	}
}

But, when we use our object in a collection, and we want to display it on a grid (for example), our cell is empty, and when we display our object, the array attribute is empty too. 🙁
:

MiniMax_MyModule_Model_MyObject Object
                (
                    [_myarray:protected] => Array
                        (
                        )

                    [_data:protected] => Array
                        (
                        )

                    [_hasDataChanges:protected] => 1
                    [_origData:protected] => 
                    [_idFieldName:protected] => 
                    [_isDeleted:protected] => 
                    [_oldFieldsMap:protected] => Array
                        (
                        )

                    [_syncFieldsMap:protected] => Array
                        (
                        )

                )

I tried a lot of solutions, but nothing… For example, I tried to override the __construct() function, but it failed too. So, I will show you how to succeed in thing, look at this code :

class MiniMax_MyModule_Model_MyObject extends Varien_Object
{
	protected $_myarray; // it's an array !?

	// A getter if you want to customize it, but not required because Magento create it with magic getters
	public function getMyArray(){ $this->myarray;}

	// The magic function
	public function setMyArray($value){
		if(is_array($this->myarray))
			$this->myarray= array_merge(array($value),$this->myarray);
		else
			$this->myarray= array($value);
	}

}

And now here is the print result of the object:

MiniMax_MyModule_Model_MyObject Object
                (
                    [_myarray:protected] => 
                    [_data:protected] => Array
                        (
                            [myarray] => Array
                                (
                                    [0] => My Value
                                    [1] => My Value 2
                                    [2] => My Value 3
                                )
                        )

                    [_hasDataChanges:protected] => 1
                    [_origData:protected] => 
                    [_idFieldName:protected] => 
                    [_isDeleted:protected] => 
                    [_oldFieldsMap:protected] => Array
                        (
                        )

                    [_syncFieldsMap:protected] => Array
                        (
                        )

                )

Hope this helps you !

Create an Array attribute in Varien Object Class on Magento
Tagged on:             

One thought on “Create an Array attribute in Varien Object Class on Magento

  • 04/05/2018 at 06:31
    Permalink

    To access ‘MiniMax_MyModule_Model_MyObject’ what changes did you do in config.xml? and how did you call the object of this class?

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

We use cookies to ensure that we give you the best experience on our website.
Ok