0

I want to save some data in to custom table.When I use static value to save data then it is going good but If I try to use dynamic data or used any calculation in between then It is going in infinite loop.

Please have look :

namespace Namespace\Module\Model;
use Magento\Framework\Model\AbstractModel;
class Test extends AbstractModel
{
    protected function _construct()
    {
        $this->_init('Namespace\Module\Model\ResourceModel\Test');
    }
    public function aftersave()
    {
        $data = $this->_objectManager->get('Namespace\Module\Model\Test')->load(10);     
       /* I also try
       $data = $this->_objectManager->create('Namespace\Module\Model\Test')->load(10);  */
       $this->customValues($data);
    } 
    public function customValues($data)
    {
         $date = new \Zend_Date('2016-09-12', self::DB_DATE_FORMAT);
         $data->setDate($date);
         $data->save(); 
    } 
}
  • you are calling same save function in after save which goes infinite – Minesh Patel May 13 '16 at 12:43
  • first it save it call after save and in after save you are calling save function again – Minesh Patel May 13 '16 at 12:44
  • @MineshPatel Yes but as I said if I use fixed value then issue is not generating. – Krupali May 13 '16 at 12:58
  • seems impossible – Minesh Patel May 13 '16 at 12:59
  • if you are saving data using model it should go in infinite for every data – Minesh Patel May 13 '16 at 13:00
2

you are going into an infinite loop because the method aftersave is a callback for the save method and it is executed (as the name says) after the save is done.
So you are doing this:

save -> aftersave -> customValues-> save -> aftersave ,.... and so on.

You should never call save in a save callback.

  • @maris If I set predefine value like : $data->setDate('2016-05-17'); $data->save(); Then it is working fine. :( – Krupali May 13 '16 at 12:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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