Beispiel #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;
    }
Beispiel #2
0
 /**
  * Save entity's attributes into the object's resource
  *
  * @param   \Magento\Framework\Object $object
  * @return $this
  * @throws \Exception
  */
 public function save(\Magento\Framework\Object $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;
 }
Beispiel #3
0
    /**
     * Save object object data
     *
     * @param \Magento\Framework\Model\AbstractModel $object
     * @return $this
     * @SuppressWarnings(PHPMD.CyclomaticComplexity)
     * @api
     */
    public function save(\Magento\Framework\Model\AbstractModel $object)
    {
        if ($object->isDeleted()) {
            return $this->delete($object);
        }
        if (!$object->hasDataChanges()) {
            return $this;
        }

        $this->beginTransaction();

        try {
            $object->validateBeforeSave();
            $object->beforeSave();
            if ($object->isSaveAllowed()) {
                $this->_serializeFields($object);
                $this->_beforeSave($object);
                $this->_checkUnique($object);
                $this->objectRelationProcessor->validateDataIntegrity($this->getMainTable(), $object->getData());
                if ($object->getId() !== null && (!$this->_useIsObjectNew || !$object->isObjectNew())) {
                    $condition = $this->_getWriteAdapter()->quoteInto($this->getIdFieldName() . '=?', $object->getId());
                    /**
                     * Not auto increment primary key support
                     */
                    if ($this->_isPkAutoIncrement) {
                        $data = $this->prepareDataForUpdate($object);
                        if (!empty($data)) {
                            $this->_getWriteAdapter()->update($this->getMainTable(), $data, $condition);
                        }
                    } else {
                        $select = $this->_getWriteAdapter()->select()->from(
                            $this->getMainTable(),
                            [$this->getIdFieldName()]
                        )->where(
                            $condition
                        );
                        if ($this->_getWriteAdapter()->fetchOne($select) !== false) {
                            $data = $this->prepareDataForUpdate($object);
                            if (!empty($data)) {
                                $this->_getWriteAdapter()->update($this->getMainTable(), $data, $condition);
                            }
                        } else {
                            $this->_getWriteAdapter()->insert(
                                $this->getMainTable(),
                                $this->_prepareDataForSave($object)
                            );
                        }
                    }
                } else {
                    $bind = $this->_prepareDataForSave($object);
                    if ($this->_isPkAutoIncrement) {
                        unset($bind[$this->getIdFieldName()]);
                    }
                    $this->_getWriteAdapter()->insert($this->getMainTable(), $bind);

                    $object->setId($this->_getWriteAdapter()->lastInsertId($this->getMainTable()));

                    if ($this->_useIsObjectNew) {
                        $object->isObjectNew(false);
                    }
                }

                $this->unserializeFields($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;
    }