Archive for July, 2010
Inspecting the contents of Magento models using debug() vs var_dump()
Saturday, July 31st, 2010
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 2:
Method 3:
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.
Posted in Magento Development | No Comments »
Why Magento? Why With Classy Llama Studios?
Tuesday, July 6th, 2010
Following is an excerpt from an e-mail I sent to a client that was considering building an eCommerce site from scratch and couldn’t understand the cost of building an effective eCommerce site on Magento considering it was “out-of-the-box” functional…
First, Magento is a primarily an eCommerce platform that secondarily is plug-and-play “out-of-the-box.” What that means is that thousands of development hours have been committed to generate the core functionality of Magento. The fact that you can plug-and-play Magento to be a functional shopping cart is an added perk that opens up new opportunities. To build an eCommerce platform that offers all of the functionality that Magento does would cost a minimum $15 million to recreate… and you wouldn’t even have the advantages that come along with it being an open-source application. So building something from scratch will undoubtedly be massively inferior.
Second, to call what we do “design” is over-simplifying the reality. Magento is a powerful foundation, but we build the house on top of that foundation that specifically accomplishes your goals. This process consists of the initial consultation and exploration, wherein we understand your business and clarify your business goals, the UI design, wherein we gather, organize, and structure the content and functionality in such a way that drives conversions most effectively (a process that doesn’t end with the build, of course), the graphic design, wherein we make the UI beautiful, consistent, and attractive to the target audience/s, which further drives conversions. Then we take the graphic design and build it to interface correctly on the Magento platform and with web browsers. Then we train you and your team to manage the Magento admin effectively so that you aren’t bound by a “webmaster” to add products, change images, descriptions, videos, launch promotions, give discounts, change pricing, and on and on and on. We want you to be in control of your e-commerce site as much as possible.
Third, when we build the site, we do so cognizant of potential future developments to ensure we don’t box in your options. We build everything using SVN, which is the first line of defense against anything subpar making it live. The second line of defense is our staging server, which gives us the closest thing to a live environment without it being publicly available. (Read about our Three Lines of Development Defense)
Much of your value is already received by the time we’ve generated the graphic design. The plans generated to build the house are critical to the ultimate success of the construction. The construction needs to be quality, but there is a lot more competition in the ability to build a house than to design the house to accomplish the goals of the owners. Don’t get me wrong; we build very, very well. But without sharp, intelligent design, building is worthless. You’re an eCommerce firm. You need an eCommerce business, not a well-coded website. We can talk more about this in our next dialogue, but I hope that provides you with some perspective on why the cost can be so high. We do what we do very well, and we help businesses experience more success than they could achieve without us. That’s what we’re offering you. (Read more about our Magento design approach)
Posted in Magento | 3 Comments »


