1

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

2
  • Take a look at this article: magento.stackexchange.com/questions/436/… Commented Feb 12, 2014 at 16:05
  • @EvgeniIvanov : actaually this link gave me idea of beforesave and aftersave methods.lol. I want to know how can we use beforesave for this purpose. I have done it by aftersave method :)] Commented Feb 13, 2014 at 8:46

2 Answers 2

1

It looks like I need to answer my own question. In fact, It is not a difficult thing. Because I am close to the real answer. Here is my 'working' _aftersave() method. Take a look

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' );
        $frontendCollection=$frontendModel->getCollection()->addFieldToFilter('news_parent_id', array('eq' => $object->getId()))->getFirstItem();
        if(!$frontendCollection->isEmpty())
        {
            $frontendCollection->addData($object->getData())->save();
        }
        else
        {
            $frontendModel->setData($object->getData());
            $frontendModel->setData("news_parent_id",$object->getId());
            $frontendModel->save();
        }


      return parent::_afterSave( $object );
    }

$object holds my posted datas actually reference to the base table I think. I am loading my table2 using _getModel_method. If condition checks whether there is already an entry in table2 correspond for the item in table1. If yes, it updates that field otherwise it will set a new item in table2. Thats it.

Still I dont know how to use beforesave for this purpose. If I got a hint on this, I will definitely make an edit on this answer.

1

Please use this piece of code:

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' )->load($object->getId(), 'news_parent_id');

        $data = $object->getData();
        unset($data['id']);

        if($frontendModel->getId())
        {
            // use addData() instead of setData() to retain news_parent_id field
            $frontendModel->addData($object->getData())
                ->save();
        }
        else
        {
            $frontendModel->unsetData();
            $frontendModel->setData($object->getData())
                ->save();
        }

      return parent::_afterSave( $object );
    }
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.