Recent Posts


Generated Magento Model Code


Frustrated by Magento? Then you’ll love Commerce Bug, the must have debugging extension for anyone using Magento. Whether you’re just starting out or you’re a seasoned pro, Commerce Bug will save you and your team hours everyday. Grab a copy and start working with Magento instead of against it.

Updated for Magento 2! No Frills Magento Layout is the only Magento front end book you'll ever need. Get your copy today!

This entry is part 9 of 43 in the series Miscellaneous Magento Articles. Earlier posts include Magento Front Controller, Reinstalling Magento Modules, Clearing the Magento Cache, Magento's Class Instantiation Abstraction and Autoload, Magento Development Environment, Logging Magento's Controller Dispatch, Magento Configuration Lint, and Slides from Magento Developer's Paradise. Later posts include Magento Knowledge Base, Magento Connect Role Directories, Magento Base Directories, PHP Error Handling and Magento Developer Mode, Magento Compiler Mode, Magento: Standard OOP Still Applies, Magento: Debugging with Varien Object, Generating Google Sitemaps in Magento, IE9 fix for Magento, Magento's Many 404 Pages, Magento Quickies, Commerce Bug in Magento CE 1.6, Welcome to Magento: Pre-Innovate, Magento's Global Variable Design Patterns, Magento 2: Factory Pattern and Class Rewrites, Magento Block Lifecycle Methods, Goodnight and Goodluck, Magento Attribute Migration Generator, Fixing Magento Flat Collections with Chaos, Pulse Storm Launcher in Magento Connect, StackExchange and the Year of the Site Builder, Scaling Magento at Copious, Incremental Migration Scripts in Magento, A Better Magento 404 Page, Anatomy of the Magento PHP 5.4 Patch, Validating a Magento Connect Extension, Magento Cross Area Sessions, Review of Grokking Magento, Imagine 2014: Magento 1.9 Infinite Theme Fallback, Magento Ultimate Module Creator Review, Magento Imagine 2014: Parent/Child Themes, Early Magento Session Instantiation is Harmful, Using Squid for Local Hostnames on iPads, and Magento, Varnish, and Turpentine.

This is a quick one for advanced users, and was inspired by a recent question I answered over at StackOverflow.

One of the nice things about a well abstracted system like Magento is the opportunities it presents for meta-programming. Drop the following code in a controller action (yes, you can define an inner function in PHP)

function someAction()
        header('Content-Type: text/plain');
        $coupon = Mage::getModel('salesrule/rule')->load(1);        

        function outputValue($value)
                case 'string':
                    echo "'".str_replace("'","\\'",$value)."'";
                case 'array':
                    echo "array(";
                    foreach($value as $v)
                        echo ",";
                    echo ")";
                case 'NULL':
                    echo 'NULL';
                    echo "'can\'t handle ".gettype($value)."'";


        echo '$model';
        foreach($coupon->getData() as $key=>$value)

            echo '->set';
            echo str_replace(' ', '',ucwords(str_replace('_', ' ', $key)));
            echo '(';

            echo ')';
            echo "\n";


Run this code and you’ll get some auto-generated code for the creation/updating of a Magento salesrule/rule Model (assuming you have a Model with an id of 1)

Originally published May 10, 2010
Series Navigation<< Slides from Magento Developer’s ParadiseMagento Knowledge Base >>

Copyright © Alana Storm 1975 – 2023 All Rights Reserved

Originally Posted: 10th May 2010

email hidden; JavaScript is required