1

I need to update a column based on another column (max value) in a different table in Magento. If the updated value is not greater than the max value then update other wise not. By Updating I meant it includes a arithemetic operation, eg Increment.

How to do it in Magento Way instead of writing a Mysql query ?

ted
  • 421
  • 1
  • 5
  • 16
  • 1
    For updating with an arithmetic operation, see http://magento.stackexchange.com/questions/57470/decrement-value-instead-of-setting-it-like-number-number-1-is-it-possible/57476#57476 – Fabian Schmengler Dec 16 '15 at 11:12

1 Answers1

1

it's probably the easiest to add this to the _prepareDataForSave method in your resource model. The Log module has a nice example of that in Mage_Log_Model_Resource_Visitor

So, if you do something like

$model = Mage::getModel('[namespace]_[module]/mycustommodel')->setData([
   'foo' => 'bar'
])->save();

and add the method to the right resource model it would look something like

class [Namespace]_[Module]_Resource_Mycustommodel extends Mage_Core_Model_Resource_Db_Abstract
{

    /**
     * Prepare data for save
     *
     * @param Mage_Core_Model_Abstract $data
     * @return array
     */
    protected function _prepareDataForSave($data)
    {
        return array(
            'foo' => $data->getBar(),
            'increment_thingy' => '[your custom value]'
        );
    }
}
Sander Mangel
  • 37,528
  • 5
  • 80
  • 148