/** * Prepare data to be saved to database * @param \Magento\Framework\Model\AbstractModel|\Magento\Framework\Object $object * * @return $this */ protected function _beforeSave(\Magento\Framework\Model\AbstractModel $object) { if ($object->isObjectNew()) { $object->setCreatedAt($this->dateTime->formatDate(true)); } $object->setUpdatedAt($this->dateTime->formatDate(true)); return $this; }
/** * Set created date * * @param \Magento\Framework\Model\AbstractModel $object * @return $this */ public function beforeSave($object) { $attributeCode = $this->getAttribute()->getAttributeCode(); if ($object->isObjectNew() && $object->getData($attributeCode) === null) { $object->setData($attributeCode, gmdate(\Magento\Framework\Stdlib\DateTime::DATETIME_PHP_FORMAT)); } return $this; }
/** * Before saving the object, add the created or updated times * * @param \Magento\Framework\Model\AbstractModel $object * @return $this */ protected function _beforeSave(\Magento\Framework\Model\AbstractModel $object) { if ($object->isObjectNew() && !$object->hasCreationTime()) { $object->setCreationTime($this->_date->gmtDate()); } $object->setUpdateTime($this->_date->gmtDate()); return parent::_beforeSave($object); }
/** * Invalidate indexer on store group save * * @param \Magento\Store\Model\Resource\Group $subject * @param \Closure $proceed * @param \Magento\Framework\Model\AbstractModel $group * * @return \Magento\Store\Model\Resource\Group * @SuppressWarnings(PHPMD.UnusedFormalParameter) */ public function aroundSave(\Magento\Store\Model\Resource\Group $subject, \Closure $proceed, \Magento\Framework\Model\AbstractModel $group) { $needInvalidation = !$group->isObjectNew() && $group->dataHasChangedFor('website_id'); $result = $proceed($group); if ($needInvalidation) { $this->indexerRegistry->get(Fulltext::INDEXER_ID)->invalidate(); } return $result; }
/** * Invalidate indexer on store view save * * @param \Magento\Store\Model\ResourceModel\Store $subject * @param \Closure $proceed * @param \Magento\Framework\Model\AbstractModel $store * * @return \Magento\Store\Model\ResourceModel\Store * @SuppressWarnings(PHPMD.UnusedFormalParameter) */ public function aroundSave(\Magento\Store\Model\ResourceModel\Store $subject, \Closure $proceed, \Magento\Framework\Model\AbstractModel $store) { $needInvalidation = $store->isObjectNew(); $result = $proceed($store); if ($needInvalidation) { $this->indexerRegistry->get(Fulltext::INDEXER_ID)->invalidate(); } return $result; }
/** * Set template type, added at and modified at time * * @param \Magento\Framework\Model\AbstractModel $object * @return $this */ protected function _beforeSave(AbstractModel $object) { if ($object->isObjectNew()) { $object->setAddedAt($this->dateTime->formatDate(true)); } $object->setModifiedAt($this->dateTime->formatDate(true)); $object->setTemplateType((int) $object->getTemplateType()); return parent::_beforeSave($object); }
/** * Invalidate indexer on searchable attribute delete * * @param \Magento\Catalog\Model\Resource\Attribute $subject * @param \Closure $proceed * @param \Magento\Framework\Model\AbstractModel $attribute * * @return \Magento\Catalog\Model\Resource\Attribute * @SuppressWarnings(PHPMD.UnusedFormalParameter) */ public function aroundDelete(\Magento\Catalog\Model\Resource\Attribute $subject, \Closure $proceed, \Magento\Framework\Model\AbstractModel $attribute) { $needInvalidation = !$attribute->isObjectNew() && $attribute->getIsSearchable(); $result = $proceed($attribute); if ($needInvalidation) { $this->indexerRegistry->get(Fulltext::INDEXER_ID)->invalidate(); } return $result; }
/** * Set created date * * @param \Magento\Framework\Model\AbstractModel $object * @return $this */ public function beforeSave($object) { $attributeCode = $this->getAttribute()->getAttributeCode(); if ($object->isObjectNew() && $object->getData($attributeCode) === null) { //$object->setData($attributeCode, $this->dateTime->gmtDate()); $object->setData($attributeCode, gmdate('Y-m-d H:i:s')); } return $this; }
/** * Prepare data for save * * @param \Magento\Framework\Model\AbstractModel $object * @return array */ protected function _prepareDataForSave(\Magento\Framework\Model\AbstractModel $object) { $currentTime = $this->dateTime->now(); if ((!$object->getId() || $object->isObjectNew()) && !$object->getCreatedAt()) { $object->setCreatedAt($currentTime); } $object->setUpdatedAt($currentTime); $data = parent::_prepareDataForSave($object); return $data; }
/** * Set date of last update * * @param \Magento\Framework\Model\AbstractModel $object * @return \Magento\Framework\Model\ResourceModel\Db\AbstractDb */ protected function _beforeSave(AbstractModel $object) { /* @var $object \ClassyLlama\AvaTax\Model\Queue */ $date = $this->dateTime->gmtDate(); if ($object->isObjectNew() && !$object->getCreatedAt()) { $object->setCreatedAt($date); } else { $object->setUpdatedAt($date); } return parent::_beforeSave($object); }
/** * @param \Magento\Store\Model\ResourceModel\Group $object * @param callable $proceed * @param AbstractModel $group * @return \Magento\Store\Model\ResourceModel\Group * @SuppressWarnings(PHPMD.UnusedFormalParameter) */ public function aroundSave(\Magento\Store\Model\ResourceModel\Group $object, \Closure $proceed, AbstractModel $group) { $originGroup = $group; $result = $proceed($originGroup); if (!$group->isObjectNew() && ($group->dataHasChangedFor('website_id') || $group->dataHasChangedFor('root_category_id'))) { $this->storeManager->reinitStores(); foreach ($group->getStoreIds() as $storeId) { $this->urlPersist->deleteByData([UrlRewrite::STORE_ID => $storeId]); } $this->urlPersist->replace($this->generateCategoryUrls($group->getRootCategoryId(), $group->getStoreIds())); $this->urlPersist->replace($this->generateProductUrls($group->getWebsiteId(), $group->getOrigData('website_id'))); } return $result; }
/** * Process post data before saving * * @param \Magento\Framework\Model\AbstractModel $object * @return $this * @throws \Magento\Framework\Exception\LocalizedException */ protected function _beforeSave(\Magento\Framework\Model\AbstractModel $object) { /* if (!$this->isValidPostLink_url($object)) { throw new \Magento\Framework\Exception\LocalizedException( __('The post URL key contains capital letters or disallowed symbols.') ); } if ($this->isNumericPostLink_url($object)) { throw new \Magento\Framework\Exception\LocalizedException( __('The post URL key cannot be made of only numbers.') ); }*/ if ($object->isObjectNew() && !$object->hasCreationTime()) { $object->setCreationTime($this->_date->gmtDate()); } $object->setUpdateTime($this->_date->gmtDate()); return parent::_beforeSave($object); }
/** * @param \Magento\Sales\Model\Spi\InvoiceResourceInterface $subject * @param \Closure $proceed * * I include both the extended AbstractModel and implemented Interface here for the IDE's benefit * @param \Magento\Framework\Model\AbstractModel|\Magento\Sales\Api\Data\InvoiceInterface $entity * @return \Magento\Sales\Model\Spi\InvoiceResourceInterface * @throws \Magento\Framework\Exception\CouldNotSaveException */ public function aroundSave(InvoiceResourceInterface $subject, \Closure $proceed, AbstractModel $entity) { // Check to see if this is a newly created entity and store the determination for later evaluation after // the entity is saved via plugin closure. After the entity is saved it will not be listed as new any longer. $isObjectNew = $entity->isObjectNew(); // Save AvaTax extension attributes if ($this->avaTaxConfig->isModuleEnabled($entity->getStoreId())) { // check to see if any extension attributes exist and set them on the model for saving to the db $extensionAttributes = $entity->getExtensionAttributes(); if ($extensionAttributes && $extensionAttributes->getAvataxIsUnbalanced() !== null) { $entity->setData('avatax_is_unbalanced', $extensionAttributes->getAvataxIsUnbalanced()); } if ($extensionAttributes && $extensionAttributes->getBaseAvataxTaxAmount() !== null) { $entity->setData('base_avatax_tax_amount', $extensionAttributes->getBaseAvataxTaxAmount()); } // Updating a field to trigger a change to the record when avatax_is_unbalanced and base_avatax_tax_amount // are both false or 0 which evaluate the same as null in the isModified check if ($extensionAttributes && ($extensionAttributes->getAvataxIsUnbalanced() !== null && ($entity->getOrigData('avatax_is_unbalanced') === null || $extensionAttributes->getAvataxIsUnbalanced() != $entity->getOrigData('avatax_is_unbalanced')) || $extensionAttributes->getBaseAvataxTaxAmount() !== null && ($entity->getOrigData('base_avatax_tax_amount') === null || $extensionAttributes->getBaseAvataxTaxAmount() != $entity->getOrigData('base_avatax_tax_amount')))) { $entity->setUpdatedAt($this->dateTime->gmtDate()); } } /** @var \Magento\Sales\Model\Spi\InvoiceResourceInterface $resultEntity */ $resultEntity = $proceed($entity); /** @var \Magento\Sales\Model\Order $order */ $order = $entity->getOrder(); $isVirtual = $order->getIsVirtual(); $address = $isVirtual ? $entity->getBillingAddress() : $entity->getShippingAddress(); $storeId = $entity->getStoreId(); // Queue the entity to be sent to AvaTax if ($this->avaTaxConfig->isModuleEnabled($entity->getStoreId()) && $this->avaTaxConfig->getTaxMode($entity->getStoreId()) == Config::TAX_MODE_ESTIMATE_AND_SUBMIT && $this->avaTaxConfig->isAddressTaxable($address, $storeId)) { // Add this entity to the avatax processing queue if this is a new entity if ($isObjectNew) { /** @var Queue $queue */ $queue = $this->queueFactory->create(); $queue->build($entity->getStoreId(), Queue::ENTITY_TYPE_CODE_INVOICE, $entity->getEntityId(), $entity->getIncrementId(), Queue::QUEUE_STATUS_PENDING); $queue->save(); $this->avaTaxLogger->debug(__('Added entity to the queue'), ['queue_id' => $queue->getId(), 'entity_type_code' => Queue::ENTITY_TYPE_CODE_INVOICE, 'entity_id' => $entity->getEntityId()]); } } return $resultEntity; }
/** * Process post data before saving * * @param \Magento\Framework\Model\AbstractModel $object * @return $this * @throws \Magento\Framework\Exception\LocalizedException */ protected function _beforeSave(\Magento\Framework\Model\AbstractModel $object) { // if (!$this->isValidPostUrlKey($object)) { // throw new \Magento\Framework\Exception\LocalizedException( // __('The post URL key contains capital letters or disallowed symbols.') // ); // } // // if ($this->isNumericPostUrlKey($object)) { // throw new \Magento\Framework\Exception\LocalizedException( // __('The post URL key cannot be made of only numbers.') // ); // } // $object->setData('photo_url', 'fefefefewfergrtgtrh'); // throw new \Magento\Framework\Exception\LocalizedException( // __('Alert!! '.$object->getData('photo_url')) // ); if ($object->isObjectNew() && !$object->hasCreationTime()) { $object->setCreationTime($this->_date->gmtDate()); } $object->setUpdateTime($this->_date->gmtDate()); return parent::_beforeSave($object); }
/** * before save callback * * @param \Magento\Framework\Model\AbstractModel|\Mageplaza\Blog\Model\Topic $object * @return $this */ protected function _beforeSave(\Magento\Framework\Model\AbstractModel $object) { $object->setUpdatedAt($this->date->date()); if ($object->isObjectNew()) { $object->setCreatedAt($this->date->date()); } //Check Url Key if ($object->isObjectNew()) { $count = 0; $objName = $object->getName(); if ($object->getUrlKey()) { $urlKey = $object->getUrlKey(); } else { $urlKey = $this->generateUrlKey($objName, $count); } while ($this->checkUrlKey($urlKey)) { $count++; $urlKey = $this->generateUrlKey($urlKey, $count); } $object->setUrlKey($urlKey); } else { $objectId = $object->getId(); $count = 0; $objName = $object->getName(); if ($object->getUrlKey()) { $urlKey = $object->getUrlKey(); } else { $urlKey = $this->generateUrlKey($objName, $count); } while ($this->checkUrlKey($urlKey, $objectId)) { $count++; $urlKey = $this->generateUrlKey($urlKey, $count); } $object->setUrlKey($urlKey); } return parent::_beforeSave($object); }
/** * Check if object is new * * @param \Magento\Framework\Model\AbstractModel $object * @return bool */ protected function isObjectNotNew(\Magento\Framework\Model\AbstractModel $object) { return $object->getId() !== null && (!$this->_useIsObjectNew || !$object->isObjectNew()); }
/** * before save callback * * @param \Magento\Framework\Model\AbstractModel|\Mageplaza\Blog\Model\Category $object * @return $this */ protected function _beforeSave(\Magento\Framework\Model\AbstractModel $object) { $object->setUpdatedAt($this->date->date()); if ($object->isObjectNew()) { $object->setCreatedAt($this->date->date()); } /** @var \Mageplaza\Blog\Model\Category $object */ parent::_beforeSave($object); if (!$object->getChildrenCount()) { $object->setChildrenCount(0); } if ($object->isObjectNew()) { if ($object->getPosition() === null) { $object->setPosition($this->getMaxPosition($object->getPath()) + 1); } $path = explode('/', $object->getPath()); $level = count($path) - ($object->getId() ? 1 : 0); $toUpdateChild = array_diff($path, [$object->getId()]); if (!$object->hasPosition()) { $object->setPosition($this->getMaxPosition(implode('/', $toUpdateChild)) + 1); } if (!$object->hasLevel()) { $object->setLevel($level); } if (!$object->hasParentId() && $level && !$object->getInitialSetupFlag()) { $object->setParentId($path[$level - 1]); } if (!$object->getId() && !$object->getInitialSetupFlag()) { $object->setPath($object->getPath() . '/'); } if (!$object->getInitialSetupFlag()) { $this->getConnection()->update($this->getMainTable(), ['children_count' => new \Zend_Db_Expr('children_count+1')], ['category_id IN(?)' => $toUpdateChild]); } } //Check Url Key if ($object->isObjectNew()) { $count = 0; $objName = $object->getName(); if ($object->getUrlKey()) { $urlKey = $object->getUrlKey(); } else { $urlKey = $this->generateUrlKey($objName, $count); } while ($this->checkUrlKey($urlKey)) { $count++; $urlKey = $this->generateUrlKey($urlKey, $count); } $object->setUrlKey($urlKey); } else { $objectId = $object->getId(); $count = 0; $objName = $object->getName(); if ($object->getUrlKey()) { $urlKey = $object->getUrlKey(); } else { $urlKey = $this->generateUrlKey($objName, $count); } while ($this->checkUrlKey($urlKey, $objectId)) { $count++; $urlKey = $this->generateUrlKey($urlKey, $count); } $object->setUrlKey($urlKey); } return $this; }
/** * Save attribute/form relations after attribute save * * @param \Magento\Framework\Model\AbstractModel $object * @return $this */ protected function _afterSave(AbstractModel $object) { $forms = $object->getData('used_in_forms'); $adapter = $this->_getWriteAdapter(); if (is_array($forms)) { $where = array('attribute_id=?' => $object->getId()); $adapter->delete($this->_getFormAttributeTable(), $where); $data = array(); foreach ($forms as $formCode) { $data[] = array('form_code' => $formCode, 'attribute_id' => (int) $object->getId()); } if ($data) { $adapter->insertMultiple($this->_getFormAttributeTable(), $data); } } // update sort order if (!$object->isObjectNew() && $object->dataHasChangedFor('sort_order')) { $data = array('sort_order' => $object->getSortOrder()); $where = array('attribute_id=?' => (int) $object->getId()); $adapter->update($this->getTable('eav_entity_attribute'), $data, $where); } // save scope attributes $websiteId = (int) $object->getWebsite()->getId(); if ($websiteId) { $table = $this->_getEavWebsiteTable(); $describe = $this->_getReadAdapter()->describeTable($table); $data = array(); if (!$object->getScopeWebsiteId() || $object->getScopeWebsiteId() != $websiteId) { $data = $this->getScopeValues($object); } $data['attribute_id'] = (int) $object->getId(); $data['website_id'] = (int) $websiteId; unset($describe['attribute_id']); unset($describe['website_id']); $updateColumns = array(); foreach (array_keys($describe) as $columnName) { $data[$columnName] = $object->getData('scope_' . $columnName); $updateColumns[] = $columnName; } $adapter->insertOnDuplicate($table, $data, $updateColumns); } return parent::_afterSave($object); }
/** * Validate changes for invalidating indexer * * @param \Magento\Framework\Model\AbstractModel $store * @return bool */ protected function validate(\Magento\Framework\Model\AbstractModel $store) { return $store->isObjectNew() || $store->dataHasChangedFor('group_id'); }
/** * before save callback * * @param AbstractModel|\Sample\News\Model\Author $object * @return $this */ protected function _beforeSave(AbstractModel $object) { foreach (['dob'] as $field) { $value = !$object->getData($field) ? null : $object->getData($field); $object->setData($field, $this->dateTime->formatDate($value)); } $object->setUpdatedAt($this->date->gmtDate()); if ($object->isObjectNew()) { $object->setCreatedAt($this->date->gmtDate()); } $urlKey = $object->getData('url_key'); if ($urlKey == '') { $urlKey = $object->getName(); } $urlKey = $object->formatUrlKey($urlKey); $object->setUrlKey($urlKey); $validKey = false; while (!$validKey) { if ($this->getIsUniqueAuthorToStores($object)) { $validKey = true; } else { $parts = explode('-', $urlKey); $last = $parts[count($parts) - 1]; if (!is_numeric($last)) { $urlKey = $urlKey . '-1'; } else { $suffix = '-' . ($last + 1); unset($parts[count($parts) - 1]); $urlKey = implode('-', $parts) . $suffix; } $object->setData('url_key', $urlKey); } } return parent::_beforeSave($object); }
/** * Validate changes for invalidating indexer * * @param \Magento\Framework\Model\AbstractModel $group * @return bool */ protected function validate(\Magento\Framework\Model\AbstractModel $group) { return ($group->dataHasChangedFor('website_id') || $group->dataHasChangedFor('root_category_id')) && !$group->isObjectNew(); }
/** * Set created/modified values before user save * * @param \Magento\Framework\Model\AbstractModel $user * @return $this */ protected function _beforeSave(\Magento\Framework\Model\AbstractModel $user) { if ($user->isObjectNew()) { $user->setCreated($this->dateTime->formatDate(true)); } $user->setModified($this->dateTime->formatDate(true)); return parent::_beforeSave($user); }
/** * @param \Magento\Store\Model\ResourceModel\Store $object * @param callable $proceed * @param AbstractModel $store * @return \Magento\Store\Model\ResourceModel\Store * @SuppressWarnings(PHPMD.UnusedFormalParameter) */ public function aroundSave(\Magento\Store\Model\ResourceModel\Store $object, \Closure $proceed, AbstractModel $store) { $originStore = $store; $result = $proceed($originStore); if ($store->isObjectNew() || $store->dataHasChangedFor('group_id')) { if (!$store->isObjectNew()) { $this->urlPersist->deleteByData([UrlRewrite::STORE_ID => $store->getId()]); } $this->urlPersist->replace($this->generateCategoryUrls($store->getRootCategoryId(), $store->getId())); $this->urlPersist->replace($this->generateProductUrls($store->getWebsiteId(), $store->getOrigData('website_id'), $store->getId())); } return $result; }