Example #1
0
 /**
  * Save object object data
  *
  * @param \Magento\Framework\Model\AbstractModel $object
  * @return $this
  * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  * @throws \Exception
  * @api
  */
 public function save(\Magento\Framework\Model\AbstractModel $object)
 {
     if ($object->isDeleted()) {
         return $this->delete($object);
     }
     $this->beginTransaction();
     try {
         if (!$this->isModified($object)) {
             $this->processNotModifiedSave($object);
             $this->commit();
             $object->setHasDataChanges(false);
             return $this;
         }
         $object->validateBeforeSave();
         $object->beforeSave();
         if ($object->isSaveAllowed()) {
             $this->_serializeFields($object);
             $this->_beforeSave($object);
             $this->_checkUnique($object);
             $this->objectRelationProcessor->validateDataIntegrity($this->getMainTable(), $object->getData());
             if ($this->isObjectNotNew($object)) {
                 $this->updateObject($object);
             } else {
                 $this->saveNewObject($object);
             }
             $this->unserializeFields($object);
             $this->processAfterSaves($object);
         }
         $this->addCommitCallback([$object, 'afterCommitCallback'])->commit();
         $object->setHasDataChanges(false);
     } catch (\Exception $e) {
         $this->rollBack();
         $object->setHasDataChanges(true);
         throw $e;
     }
     return $this;
 }
 /**
  * Save entity's attributes into the object's resource
  *
  * @param  \Magento\Framework\Model\AbstractModel $object
  * @return $this
  * @throws \Exception
  */
 public function save(\Magento\Framework\Model\AbstractModel $object)
 {
     /**
      * Direct deleted items to delete method
      */
     if ($object->isDeleted()) {
         return $this->delete($object);
     }
     if (!$object->hasDataChanges()) {
         return $this;
     }
     $this->beginTransaction();
     try {
         $object->validateBeforeSave();
         $object->beforeSave();
         if ($object->isSaveAllowed()) {
             if (!$this->isPartialSave()) {
                 $this->loadAllAttributes($object);
             }
             if ($this->getEntityTable() == \Magento\Eav\Model\Entity::DEFAULT_ENTITY_TABLE && !$object->getEntityTypeId()) {
                 $object->setEntityTypeId($this->getTypeId());
             }
             $object->setParentId((int) $object->getParentId());
             $this->objectRelationProcessor->validateDataIntegrity($this->getEntityTable(), $object->getData());
             $this->_beforeSave($object);
             $this->_processSaveData($this->_collectSaveData($object));
             $this->_afterSave($object);
             $object->afterSave();
         }
         $this->addCommitCallback([$object, 'afterCommitCallback'])->commit();
         $object->setHasDataChanges(false);
     } catch (\Exception $e) {
         $this->rollBack();
         $object->setHasDataChanges(true);
         throw $e;
     }
     return $this;
 }