Magento Model Code Generation

This is a quick one for advanced users, is a recent problem, I answered StackOverflow inspired.

Magento as well as about an abstract system as one of the advantages is the opportunity it presents metaprogramming. Drops action in a controller (which is the following code, you can define an internal function in PHP)

function outputValue($value)
{
switch(gettype($value))
{
case ‘string’:
echo “‘”.str_replace(“‘”,”\\'”,$value).”‘”;
break;
case ‘array’:
echo “array(“;
foreach($value as $v)
{
outputValue($v);
echo “,”;
}
echo “)”;
break;
case ‘NULL’:
echo ‘NULL’;
break;
default:
echo “‘can\’t handle “.gettype($value).”‘”;
}

}

echo ‘$model’;
foreach($coupon->getData() as $key=>$value)
{

echo ‘->set’;
echo str_replace(‘ ‘, ”,ucwords(str_replace(‘_’, ‘ ‘, $key)));
echo ‘(‘;

outputValue($value);
echo ‘)’;
echo “\n”;
}

exit;
}

Run this code, you will get some for creating / 1 Magento salesrule / rules model assumes that you have an ID for the 11 model (updated automatically generated code)
$model->setRuleId('1')
->setName('test')
->setDescription('')
->setFromDate('2010-05-09')
->setToDate(NULL)
->setCouponCode('')
->setUsesPerCoupon('0')
->setUsesPerCustomer('0')
->setCustomerGroupIds(array('1',))
->setIsActive('1')
->setConditionsSerialized('a:6:{s:4:"type";s:32:"salesrule/rule_condition_combine";s:9:"attribute";N;s:8:"operator";N;s:5:"value";s:1:"1";s:18:"is_value_processed";N;s:10:"aggregator";s:3:"all";}')
->setActionsSerialized('a:6:{s:4:"type";s:40:"salesrule/rule_condition_product_combine";s:9:"attribute";N;s:8:"operator";N;s:5:"value";s:1:"1";s:18:"is_value_processed";N;s:10:"aggregator";s:3:"all";}')
->setStopRulesProcessing('0')
->setIsAdvanced('1')
->setProductIds('')
->setSortOrder('0')
->setSimpleAction('by_percent')
->setDiscountAmount('10.0000')
->setDiscountQty(NULL)
->setDiscountStep('0')
->setSimpleFreeShipping('0')
->setApplyToShipping('0')
->setTimesUsed('0')
->setIsRss('1')
->setWebsiteIds(array('1',))

(Based on Alanstorm Turtorial)

Comments Closed