Frage

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
 }
War es hilfreich?

Lösung

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:

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit magento.stackexchange
scroll top