I have a custom module, which uses two tables to store data in database. table 1 and table 2 are conntected by storing the id of table1 in one of the field of table2. In order to store datas to table2, I am using protected function _aftersave(). Here is my aftersave method
// this function will get executed before the model is saved but after save function has been called
protected function _afterSave( Mage_Core_Model_Abstract $object )
{
// do some model loads and checks here so that you will update existing data and not duplicate rows on editAction saves...\
$frontendModel = Mage::getModel( 'news/newsfrontend' );
//loading item in table2 with current table1 id
$frontendCollection=$frontendModel->getCollection()
->addFieldToFilter('news_parent_id', array('eq' => $object->getId()))
->getLastItem();
if(!$frontendCollection->isEmpty()) //check whether any item exist or not
{
//saving news data to existing item
$frontendCollection->setData("news_parent_id",$object->getId())->save();
$frontendCollection->setData($object->getData())->save();
}
else
{
//storing new datas to new item
$frontendModel->setData("news_parent_id",$object->getId())->save();
$frontendModel->setData($object->getData())->save();
}
return parent::_beforeSave( $object );
}
The problem that I am facing now are
1) news_parent_id
is not setting after save action. Rest datas are saving
in table2
2)Is this the correct approach to my problem?
3)How can I set news_parent_id
correctly?
4)Is there any easy way for achieving this?
5)if I am using _beforesave()
, how can I get the table1 id in order to set the news_parent_id
through it?
Please enlighten me with your ideas and suggestions. Awaiting for your response. Thanks in advance
beforesave
andaftersave
methods.lol. I want to know how can we use beforesave for this purpose. I have done it by aftersave method :)]