public function testDelete() { $this->actionValidatorMock->expects($this->any())->method('isAllowed')->will($this->returnValue(true)); $this->adapterMock->expects($this->once())->method('beginTransaction'); $this->resourceMock->expects($this->once())->method('delete'); $this->resourceMock->expects($this->once())->method('commit'); $this->model->delete(); $this->assertTrue($this->model->isDeleted()); }
/** * Tests \Magento\Framework\Object->isDeleted() */ public function testIsDeleted() { $this->assertFalse($this->model->isDeleted()); $this->model->isDeleted(); $this->assertFalse($this->model->isDeleted()); $this->model->isDeleted(true); $this->assertTrue($this->model->isDeleted()); }
/** * @inheritdoc */ public function save(\Magento\Framework\Model\AbstractModel $object) { /** * Direct deleted items to delete method */ if ($object->isDeleted()) { return $this->delete($object); } $this->beginTransaction(); try { if (!$this->isModified($object)) { $this->entityRelationComposite->processRelations($object); $this->commit(); return $this; } $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); $this->entitySnapshot->registerSnapshot($object); $object->afterSave(); $this->entityRelationComposite->processRelations($object); } $this->addCommitCallback([$object, 'afterCommitCallback'])->commit(); $object->setHasDataChanges(false); } catch (\Exception $e) { $this->rollBack(); $object->setHasDataChanges(true); throw $e; } return $this; }
/** * @inheritDoc */ public function save(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()); $this->entityManager->save(PageInterface::class, $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 Product Index data (forced save) * * @param \Magento\Framework\Model\AbstractModel $object * @return $this|\Magento\Framework\Model\Resource\Db\AbstractDb */ public function save(\Magento\Framework\Model\AbstractModel $object) { if ($object->isDeleted()) { return $this->delete($object); } $this->_serializeFields($object); $this->_beforeSave($object); $this->_checkUnique($object); $data = $this->_prepareDataForSave($object); unset($data[$this->getIdFieldName()]); $matchFields = ['product_id', 'store_id']; $this->_resourceHelper->mergeVisitorProductIndex($this->getMainTable(), $data, $matchFields); $this->unserializeFields($object); $this->_afterSave($object); return $this; }
/** * Save object object data * * @param \Magento\Framework\Model\AbstractModel $object * @return $this */ public function save(\Magento\Framework\Model\AbstractModel $object) { if ($object->isDeleted()) { return $this->delete($object); } $this->_serializeFields($object); $this->_beforeSave($object); $this->_checkUnique($object); if (!is_null($object->getId()) && (!$this->_useIsObjectNew || !$object->isObjectNew())) { $condition = $this->_getWriteAdapter()->quoteInto($this->getIdFieldName() . '=?', $object->getId()); /** * Not auto increment primary key support */ if ($this->_isPkAutoIncrement) { $data = $this->_prepareDataForSave($object); unset($data[$this->getIdFieldName()]); $this->_getWriteAdapter()->update($this->getMainTable(), $data, $condition); } else { $select = $this->_getWriteAdapter()->select()->from($this->getMainTable(), array($this->getIdFieldName()))->where($condition); if ($this->_getWriteAdapter()->fetchOne($select) !== false) { $data = $this->_prepareDataForSave($object); unset($data[$this->getIdFieldName()]); 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); return $this; }
/** * Save entity * * @param \Magento\Framework\Model\AbstractModel $object * @return $this * @throws \Exception */ public function save(\Magento\Framework\Model\AbstractModel $object) { if ($object->isDeleted()) { return $this->delete($object); } if (!$this->entitySnapshot->isModified($object)) { $this->entityRelationComposite->processRelations($object); 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()); $data = $this->_prepareDataForSave($object); unset($data[$this->getIdFieldName()]); $this->_getWriteAdapter()->update($this->getMainTable(), $data, $condition); } else { $bind = $this->_prepareDataForSave($object); 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); $this->entitySnapshot->registerSnapshot($object); $object->afterSave(); $this->entityRelationComposite->processRelations($object); } $this->addCommitCallback([$object, 'afterCommitCallback'])->commit(); $object->setHasDataChanges(false); } catch (\Exception $e) { $this->rollBack(); $object->setHasDataChanges(true); throw $e; } return $this; }
/** * 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; }