public function testIsModified() { $entityId = 1; $data = ['id' => $entityId, 'name' => 'test', 'description' => '', 'custom_not_present_attribute' => '']; $fields = ['id' => [], 'name' => [], 'description' => []]; $modifiedData = array_merge($data, ['name' => 'newName']); $this->model->expects($this->exactly(4))->method('getData')->willReturnOnConsecutiveCalls($data, $modifiedData, $modifiedData, $modifiedData); $this->model->expects($this->any())->method('getId')->willReturn($entityId); $this->entityMetadata->expects($this->exactly(4))->method('getFields')->with($this->model)->willReturn($fields); $this->entitySnapshot->registerSnapshot($this->model); $this->assertTrue($this->entitySnapshot->isModified($this->model)); $this->entitySnapshot->registerSnapshot($this->model); $this->assertFalse($this->entitySnapshot->isModified($this->model)); }
/** * 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; }
/** * Load data with filter in place * All returned rows marked as non changed to prevent unnecessary persistence operations * * @param bool $printQuery * @param bool $logQuery * @return $this */ public function loadWithFilter($printQuery = false, $logQuery = false) { $this->_beforeLoad(); $this->_renderFilters()->_renderOrders()->_renderLimit(); $this->printLogQuery($printQuery, $logQuery); $data = $this->getData(); $this->resetData(); if (is_array($data)) { foreach ($data as $row) { $item = $this->getNewEmptyItem(); if ($this->getIdFieldName()) { $item->setIdFieldName($this->getIdFieldName()); } $item->setData($row); $this->entitySnapshot->registerSnapshot($item); $this->addItem($item); } } $this->_setIsLoaded(); $this->_afterLoad(); return $this; }