Inspecting the contents of Magento models using debug() vs var_dump()

When you are developing for Magento, it is often helpful to see the contents of a Model. A model is a pretty complex object, so when you want to inspect the contents of the model, you generally don’t want to see all of it’s properties – all you’re really wanting to see is the contents of the _data property, which is the array that contains all of the attributes/fields that have been retrieved from the database.

Typically, when inspecting a model, I’ve used one of the following methods:

$model = Mage::getModel('catalog/product')->load(1);
 
# Method #1: This dumps all of the model's property's, including the _data property.
# This results in a lot of superfluous code that I don't need to see.
var_dump($model);
 
# Method #2: This only logs the data in the _data property.  The problem is, if the _data property contains
# any other models, all of the properties of those models will be dumped
var_dump($model->getData());

I recently came across the ->dump() method that is implemented in the Varien_Object class. This method recurses into the _data property of a model and returns an array of the values in the _data property, including the _data properties of the child classes. Here’s the example code:

# Method #3
var_dump($model->debug());

Note: The Varien_Object class is a class from which MANY classes in Magento extend. Most notably, all blocks and all data models extend Varien_Object. If you’ve never looked at the Varien_Object class, I’d recommend scanning/reading through the methods in that class. If you’re too lazy to read through the methods, but want to see what the debug() method does, I’ve included it at the bottom of this post.

Here are three screenshots exemplifying the difference between the different methods (note: the beautifully formatted var_dump output is a feature of the xdebug php extension):

Method 1:

Method 1 - var_dump

Method 1 - var_dump

Method 2:

Method 2 - getData

Method 2 - getData

Method 3:

Method 3 - debug

Method 3 - debug

In case you’re curious to see how the debug() method works, here’s the code from the Varien_Object class:

    /**
     * Present object data as string in debug mode
     *
     * @param mixed $data
     * @param array $objects
     * @return string
     */
    public function debug($data=null, &$objects=array())
    {
        if (is_null($data)) {
            $hash = spl_object_hash($this);
            if (!empty($objects[$hash])) {
                return '*** RECURSION ***';
            }
            $objects[$hash] = true;
            $data = $this->getData();
        }
        $debug = array();
        foreach ($data as $key=>$value) {
            if (is_scalar($value)) {
                $debug[$key] = $value;
            } elseif (is_array($value)) {
                $debug[$key] = $this->debug($value, $objects);
            } elseif ($value instanceof Varien_Object) {
                $debug[$key.' ('.get_class($value).')'] = $value->debug(null, $objects);
            }
        }
        return $debug;
    }

I hope this quick tip helps you program with models more effectively! If you have any questions, feel free to post comments below.

3 Responses to “Inspecting the contents of Magento models using debug() vs var_dump()”

  1. Curso Programador PHP…

    Inspecting the contents of Magento models using debug() vs var_dump() » Classy Llama Studios – Specializing in Magento eCommerce, Magento Development, and Magento Design…

  2. online flag store…

    [...]Inspecting the contents of Magento models using debug() vs var_dump() » Classy Llama Studios – Specializing in Magento eCommerce, Magento Development, and Magento Design[...]…

  3. Sunray Studio – создание и раскрутка сайтов…

    [...]Inspecting the contents of Magento models using debug() vs var_dump() » Classy Llama Studios – Specializing in Magento eCommerce, Magento Development, and Magento Design[...]…

Comment Form

For Code: Use the html code for < & >.  i.e. "&lt;YOUR_TAG&gt; ...your code... &lt;/YOUR_TAG&gt;"

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Call us at 417-597-3397, email us at sales@classyllama.com, or use this form to contact us:

  1. (required)
  2. (required)
 

cforms contact form by delicious:days