Question

Most of the time during the customization, we need at one time to use a getData() on an object exemple $_item = $this->getItem(); to retrieve the datas and see if we have what we are looking for, except that sometimes we come across a large number of items So that it takes a long time to load and sometimes even go to the time out.

My question is: it is possible to add a limit on the getData() ? or if you have other tricks to get the datas of an object, that would be welcome.

EDIT:

To clarify better what i want, I know that I can filter by name like getData('attribute_name') but admit that I'm looking for 20 attributes, so to not mention them all one by one. I wanted to know if there was a way to limit theme per (100) for exemple.

How do you do when you want to know the data present in some object that you don't know what it contains ?

Was it helpful?

Solution

Not sure if this is what your looking for, but to get one data-field of a large collection you can use $collection->getColumnValues('attribute_code').


Edit:

How do you do when you want to know the data present in some object that you don't know what it contains ?

When talking about debugging large objects you can also use this. It will leave out any recursive data - so you'll get no blank screen or empty log file for large objects ...

# if $item is some kind of Varien object
Mage::log($item->debug());

OTHER TIPS

when I am debugging and when a Mage::log($someObject->getData()) returns an error "max memory allowed exhausted", I start with Mage::log(array_keys($someObject->getData())); in order to know which keys will be usefull.

Then I can log using Mage::log($someObject->getData('name_of_a_key'));

I usually do this if I know what data I am expecting:

//$item is order item
$item->getData('name');

This will give you only the name of the item.

getData() feature is available in Magento in almost all kind of classes. It can be used against all flat model classes, eav model classes, model collection classes, block classes, helper classes and controller classes etc. There is no doubt developers use getData() function in order to dump all data related to the class and evaluate it.

You need to use different tricks for different kind of classes in order to limit the getData() output.

In case of collection classes, instead of loading all attribute data, you can load only those which is needed. ie instead of addAttributeToSelect('*'), you can use addAttributeToSelect(['id', 'name', 'your_required_fields']). This will eventually limit getData() output.

In case of model entity, you can use hasData('data_field_to_check) method in order to check the data. Or you can try getData('data_field_to_check'). If you are sure the data which you are enquiring is an array, then in order to get a particular value inside that array, you can try this: getData('array_field', 'key_value_to_retrieve')

Other than this, you cannot really limit the output of getData().

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top