0

As in subject - while I am working with collections should I (after filtering etc.) get data with function getData()? I've heard an opinion that is rather for debugging, not real coding.

If I use getData() it return an array and if I don't use it I still have an object, so I can use all functions from Magento core to get information I need. Dumping an array I have to use some array methods and often a loop or a few loop with some 'ifs'. I suppose it is rather better to not do it... Am I right?

1

If you're getting data from a single or small number of Magento models then why make things harder for yourself.

However on larger collections of models, where there is a LOT of data. Let's say a collection of 5000+ products. You might not want all of the Varien Object's inheireted data.It is probably more RAM and CPU friendly to use foreach($collection->toArray() as $product_data) {} to just get the basic data from the model as an array and you want to do some low level PHP logic.

I'd check out this article that explains where some of those Magento and PHP methods come from and how they work. http://alanstorm.com/magento_varien_object_debugging

If you look into \lib\Varien\Object.php You'll see some of the other methods that are available on Varien Objects, there's a few other ways to get a property in the varien object.

Some examples of getData being used.

Logging server http request data in app\code\core\Mage\Log\Model\Visitor.php

public function initServerData()
        {
            $this->addData(array(
                'server_addr'           => $this->_httpHelper->getServerAddr(true),
                'remote_addr'           => $this->_httpHelper->getRemoteAddr(true),
                'http_secure'           => Mage::app()->getStore()->isCurrentlySecure(),
                'http_host'             => $this->_httpHelper->getHttpHost(true),
                'http_user_agent'       => $this->_httpHelper->getHttpUserAgent(true),
                'http_accept_language'  => $this->_httpHelper->getHttpAcceptLanguage(true),
                'http_accept_charset'   => $this->_httpHelper->getHttpAcceptCharset(true),
                'request_uri'           => $this->_httpHelper->getRequestUri(true),
                'session_id'            => $this->_session->getSessionId(),
                'http_referer'          => $this->_httpHelper->getHttpReferer(true),
            ));

            return $this;
        }

public function saveByRequest($observer)
        {
            if ($this->_skipRequestLogging || $this->isModuleIgnored($observer)) {
                return $this;
            }

            try {
                $this->setLastVisitAt(now());
                $this->save();
                $this->_session->setVisitorData($this->getData());
            } catch (Exception $e) {
                Mage::logException($e);
            }
            return $this;
        }

A nullcheck like in app\code\core\Mage\Core\Model\App\Area.php

        if ($designChange->getData()) {
        $designPackage->setPackageName($designChange->getPackage())
            ->setTheme($designChange->getTheme());
        }
  • So to sum up you think that is ok to use getData()? – Bartosz Kubicki Jun 17 '16 at 6:19
  • I'd say it's totally ok so long as you are getting or checking on what's in the raw array of data. – asherrard Jun 17 '16 at 14:59
  • Please make your path's POSIX compliant, a.k.a use forward slashes – Ray Foss Oct 27 '16 at 13:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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