2

I have Create two event model save before and after i get specific object and set flag in before model and get in after model. for example in before model

    public function execute(\Magento\Framework\Event\Observer $observer)
    {   
            $obj = $observer->getEvent()->getObject();
            if ($obj instanceof \Magento\Customer\Model\Customer)
            {    
             $this->isNew = is_null($obj->getId());
             //var_dump($this->isNew);die; //true     
             return;
            }
}

in after model

 public function execute(\Magento\Framework\Event\Observer $observer)
 {   
     var_dump($this->isNew); die; //Null
 }
  • You can also set registry and get registry to identify your flag – Keyur Shah Jul 4 '16 at 11:20
1

Magento 2 provides a method for this: isObjectNew()

Thus you can do the following in both beforeSave() and afterSave() methods:

if ($this->isObjectNew()) {
    // New object
}

Examples from Magento 2 core files:

  • i need to pass flag in specific object so i add it in variable i don't need to check object is new. – Pratik Jul 4 '16 at 11:52
  • @Pratik well your flag is called $isNew so I assumed that's what you wanted to do – Raphael at Digital Pianism Jul 4 '16 at 11:59
  • yes you are right i found it thx. – Pratik Jul 4 '16 at 12:03

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.