1

I have created a module which saves some data into the custom tables. Both tables have "created_at" and "updated_at" fields in them. But when I save the data into any if these tables "created_at" field does not set automatically.

Here is my code: Sql script:

->addColumn('created_at', Varien_Db_Ddl_Table::TYPE_TIMESTAMP, null, array(
        'nullable' => true,
    ), 'Created At')
    ->addColumn('updated_at', Varien_Db_Ddl_Table::TYPE_TIMESTAMP, null, array(
        'nullable' => true,
    ), 'Updated At')

Model.php

class my_Module extends Mage_Core_Model_Abstract
{
        public function addMyList($customerId, $name)
        {
            $this->setData(
                array(
                    'b2b_customer_id' => $customerId,
                    'name' => $name,
                )
            );
            $this->save();
            return;
        }
}

I have looked into other posts but most of them set this "created_at" filed along with the rest of the data in the function. Which is not what exactly I want because I think Magento should handle it itself.

But please let me know if I am thinking wrong or the code is wrong somehow.

1 Answer 1

1

you can add this method to your main model and you don't care anymore about setting created_at. Every time you call save it will be set automatically.
It also handles updated_at.

protected function _beforeSave() {
    parent::_beforeSave();
    $now = Mage::getSingleton('core/date')->gmtDate();
    if ($this->isObjectNew()){
        $this->setCreatedAt($now);
    }
    $this->setUpdatedAt($now);
    return $this;
}
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.