/** * Save relations for Customer * * @param \Magento\Framework\Model\AbstractModel $customer * @return void * @SuppressWarnings(PHPMD.CyclomaticComplexity) */ public function processRelation(\Magento\Framework\Model\AbstractModel $customer) { $defaultBillingId = $customer->getData('default_billing'); $defaultShippingId = $customer->getData('default_shipping'); /** @var \Magento\Customer\Model\Address $address */ foreach ($customer->getAddresses() as $address) { if ($address->getData('_deleted')) { if ($address->getId() == $defaultBillingId) { $customer->setData('default_billing', null); } if ($address->getId() == $defaultShippingId) { $customer->setData('default_shipping', null); } $removedAddressId = $address->getId(); $address->delete(); // Remove deleted address from customer address collection $customer->getAddressesCollection()->removeItemByKey($removedAddressId); } else { $address->setParentId($customer->getId())->setStoreId($customer->getStoreId())->setIsCustomerSaveTransaction(true)->save(); if (($address->getIsPrimaryBilling() || $address->getIsDefaultBilling()) && $address->getId() != $defaultBillingId) { $customer->setData('default_billing', $address->getId()); } if (($address->getIsPrimaryShipping() || $address->getIsDefaultShipping()) && $address->getId() != $defaultShippingId) { $customer->setData('default_shipping', $address->getId()); } } } $changedAddresses = []; $changedAddresses['default_billing'] = $customer->getData('default_billing'); $changedAddresses['default_shipping'] = $customer->getData('default_shipping'); $customer->getResource()->getConnection()->update($customer->getResource()->getTable('customer_entity'), $changedAddresses, $customer->getResource()->getConnection()->quoteInto('entity_id = ?', $customer->getId())); }
/** * Add customer group ids and website ids to rule data after load * * @param AbstractModel $object * @return $this */ protected function _afterLoad(AbstractModel $object) { $object->setData('customer_group_ids', (array) $this->getCustomerGroupIds($object->getId())); $object->setData('website_ids', (array) $this->getWebsiteIds($object->getId())); parent::_afterLoad($object); return $this; }
/** * Saving test data to database * @return mixed */ protected function saveTestData() { foreach ($this->getTestData() as $key => $value) { $this->model->setData($key, $value); } $this->model->save(); return $this->model->getId(); }
/** * @param \Magento\Framework\Model\AbstractModel $object * @param string $dateIdentifier * @return void */ private function resolveDate(\Magento\Framework\Model\AbstractModel $object, $dateIdentifier) { $date = $object->getData($dateIdentifier); if ($date instanceof \DateTime) { $object->setData($dateIdentifier, $date->format('Y-m-d H:i:s')); } elseif (!is_string($date) || empty($date)) { $object->setData($dateIdentifier, null); } }
public function testUpdateStoredData() { $this->model->setData(['id' => 1000, 'name' => 'Test Name']); $this->assertEmpty($this->model->getStoredData()); $this->model->afterLoad(); $this->assertEquals($this->model->getData(), $this->model->getStoredData()); $this->model->setData('value', 'Test Value'); $this->model->afterSave(); $this->assertEquals($this->model->getData(), $this->model->getStoredData()); $this->model->afterDelete(); $this->assertEmpty($this->model->getStoredData()); }
/** * Method to run after load * * @param \Magento\Framework\Model\AbstractModel $object * @return $this */ protected function _afterLoad(\Magento\Framework\Model\AbstractModel $object) { $select = $this->_getReadAdapter()->select()->from($this->getTable('checkout_agreement_store'), ['store_id'])->where('agreement_id = :agreement_id'); if ($stores = $this->_getReadAdapter()->fetchCol($select, [':agreement_id' => $object->getId()])) { $object->setData('store_id', $stores); } return parent::_afterLoad($object); }
/** * 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; }
/** * {@inheritdoc} */ protected function _beforeSave(AbstractModel $tag) { /** @var \Mirasvit\Blog\Model\Tag $tag */ if (!$tag->getData('url_key')) { $tag->setData('url_key', $this->filter->translitUrl($tag->getName())); } return parent::_beforeSave($tag); }
/** * Perform actions after object load * * @param \Magento\Widget\Model\Widget\Instance $object * @return $this */ protected function _afterLoad(AbstractModel $object) { $connection = $this->getConnection(); $select = $connection->select()->from($this->getTable('widget_instance_page'))->where('instance_id = ?', (int) $object->getId()); $result = $connection->fetchAll($select); $object->setData('page_groups', $result); return parent::_afterLoad($object); }
/** * Perform actions before object save * * @param \Magento\Framework\Model\AbstractModel $object * @return $this */ protected function _beforeSave(AbstractModel $object) { $validateRules = $object->getData('validate_rules'); if (is_array($validateRules)) { $object->setData('validate_rules', serialize($validateRules)); } return parent::_beforeSave($object); }
/** * Before save handler * * @param \Magento\Cms\Model\ResourceModel\Page $subject * @param \Magento\Framework\Model\AbstractModel $object * * @return void * @SuppressWarnings(PHPMD.UnusedFormalParameter) */ public function beforeSave(\Magento\Cms\Model\ResourceModel\Page $subject, \Magento\Framework\Model\AbstractModel $object) { /** @var $object \Magento\Cms\Model\Page */ $urlKey = $object->getData('identifier'); if ($urlKey === '' || $urlKey === null) { $object->setData('identifier', $this->cmsPageUrlPathGenerator->generateUrlKey($object)); } }
/** * Perform operations after object load * * @param \Magento\Framework\Model\AbstractModel $object * @return $this */ protected function _afterLoad(\Magento\Framework\Model\AbstractModel $object) { if ($object->getId()) { $productIds = $this->lookupProductIds($object->getId()); $productId = implode(',', $productIds); $object->setData('product_id', $productId); } return parent::_afterLoad($object); }
/** * Restore old attribute value * * @param \Magento\Framework\Model\AbstractModel $model * @param mixed $oldAttrValue * @return void */ protected function _restoreOldAttrValue(\Magento\Framework\Model\AbstractModel $model, $oldAttrValue) { $attrCode = $this->getAttribute(); if ($oldAttrValue === null) { $model->unsetData($attrCode); } else { $model->setData($attrCode, $oldAttrValue); } }
/** * 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; }
/** * Custom load model only by query text (skip synonym for) * * @param AbstractModel $object * @param string $value * @return $this */ public function loadByQueryText(AbstractModel $object, $value) { $select = $this->getConnection()->select()->from($this->getMainTable())->where('query_text = ?', $value)->where('store_id = ?', $object->getStoreId())->limit(1); $data = $this->getConnection()->fetchRow($select); if ($data) { $object->setData($data); $this->_afterLoad($object); } return $this; }
/** * Custom load model: Get data by store view id * * @param AbstractModel $object * @param int $value * @return $this */ public function loadByStoreViewId(AbstractModel $object, $value) { $select = $this->getConnection()->select()->from($this->getMainTable())->where('store_id = ?', $value); $data = $this->getConnection()->fetchAll($select); if ($data) { $object->setData($data); $this->_afterLoad($object); } return $this; }
/** * @param AbstractModel $object * @param string $value * @return $this */ protected function loadByCode($object, $value) { $select = $this->getConnection()->select()->from($this->getMainTable())->where('code = ?', $value, $value)->limit(1); $data = $this->getConnection()->fetchRow($select); if ($data) { $object->setData($data); $this->_afterLoad($object); } return $this; }
/** * Tests \Magento\Framework\Object->setOrigData() */ public function testOrigData() { $data = ['key1' => 'value1', 'key2' => 'value2']; $this->model->setData($data); $this->model->setOrigData(); $this->model->setData('key1', 'test'); $this->assertTrue($this->model->dataHasChangedFor('key1')); $this->assertEquals($data, $this->model->getOrigData()); $this->model->setOrigData('key1', 'test'); $this->assertEquals('test', $this->model->getOrigData('key1')); }
/** * Load * * @param \Magento\Framework\Model\AbstractModel $object * @param String $value * @param String $field * @return array|$this */ public function load(\Magento\Framework\Model\AbstractModel $object, $value, $field = null) { if (is_string($value)) { $select = $this->_getReadAdapter()->select()->from($this->getMainTable())->where($this->getMainTable() . '.string=:tr_string'); $result = $this->_getReadAdapter()->fetchRow($select, array('tr_string' => $value)); $object->setData($result); $this->_afterLoad($object); return $result; } else { return parent::load($object, $value, $field); } }
/** * Serialize additional information, if any * * @param \Magento\Framework\Model\AbstractModel $transaction * @return $this * @throws \Magento\Framework\Model\Exception */ protected function _beforeSave(\Magento\Framework\Model\AbstractModel $transaction) { $txnId = $transaction->getData('txn_id'); $idFieldName = $this->getIdFieldName(); // make sure unique key won't cause trouble if ($transaction->isFailsafe()) { $autoincrementId = (int) $this->_lookupByTxnId($txnId, $idFieldName); if ($autoincrementId) { $transaction->setData($idFieldName, $autoincrementId)->isObjectNew(false); } } return parent::_beforeSave($transaction); }
/** * Load an object by entity type and store * * @param Object|\Magento\Framework\Model\AbstractModel $object * @param int $entityTypeId * @param int $storeId * @return bool */ public function loadByEntityStore(AbstractModel $object, $entityTypeId, $storeId) { $connection = $this->getConnection(); $bind = [':entity_type_id' => $entityTypeId, ':store_id' => $storeId]; $select = $connection->select()->from($this->getMainTable())->forUpdate(true)->where('entity_type_id = :entity_type_id')->where('store_id = :store_id'); $data = $connection->fetchRow($select, $bind); if (!$data) { return false; } $object->setData($data); $this->_afterLoad($object); return true; }
protected function _afterSave(\Magento\Framework\Model\AbstractModel $object) { // TODO test it on magento 2.0 // fix for Varien\Db\Adapter\Pdo\Mysql::prepareColumnValue // an empty string cannot be saved -> NULL is saved instead // for Magento version > 1.6.x.x foreach ($object->getData() as $key => $value) { if ($value instanceof \Zend_Db_Expr && $value->__toString() === '\'\'') { $object->setData($key, ''); } } return parent::_afterSave($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; }
public function generate(\Magento\Framework\Model\AbstractModel $object) { if ($object->getData('identifier')) { return; } $identifier = trim($object->getData('title')); if (!$identifier) { return; } $from = ['Á', 'À', 'Â', 'Ä', 'Ă', 'Ā', 'Ã', 'Å', 'Ą', 'Æ', 'Ć', 'Ċ', 'Ĉ', 'Č', 'Ç', 'Ď', 'Đ', 'Ð', 'É', 'È', 'Ė', 'Ê', 'Ë', 'Ě', 'Ē', 'Ę', 'Ə', 'Ġ', 'Ĝ', 'Ğ', 'Ģ', 'á', 'à', 'â', 'ä', 'ă', 'ā', 'ã', 'å', 'ą', 'æ', 'ć', 'ċ', 'ĉ', 'č', 'ç', 'ď', 'đ', 'ð', 'é', 'è', 'ė', 'ê', 'ë', 'ě', 'ē', 'ę', 'ə', 'ġ', 'ĝ', 'ğ', 'ģ', 'Ĥ', 'Ħ', 'I', 'Í', 'Ì', 'İ', 'Î', 'Ï', 'Ī', 'Į', 'IJ', 'Ĵ', 'Ķ', 'Ļ', 'Ł', 'Ń', 'Ň', 'Ñ', 'Ņ', 'Ó', 'Ò', 'Ô', 'Ö', 'Õ', 'Ő', 'Ø', 'Ơ', 'Œ', 'ĥ', 'ħ', 'ı', 'í', 'ì', 'i', 'î', 'ï', 'ī', 'į', 'ij', 'ĵ', 'ķ', 'ļ', 'ł', 'ń', 'ň', 'ñ', 'ņ', 'ó', 'ò', 'ô', 'ö', 'õ', 'ő', 'ø', 'ơ', 'œ', 'Ŕ', 'Ř', 'Ś', 'Ŝ', 'Š', 'Ş', 'Ť', 'Ţ', 'Þ', 'Ú', 'Ù', 'Û', 'Ü', 'Ŭ', 'Ū', 'Ů', 'Ų', 'Ű', 'Ư', 'Ŵ', 'Ý', 'Ŷ', 'Ÿ', 'Ź', 'Ż', 'Ž', 'ŕ', 'ř', 'ś', 'ŝ', 'š', 'ş', 'ß', 'ť', 'ţ', 'þ', 'ú', 'ù', 'û', 'ü', 'ŭ', 'ū', 'ů', 'ų', 'ű', 'ư', 'ŵ', 'ý', 'ŷ', 'ÿ', 'ź', 'ż', 'ž', 'А', 'Б', 'В', 'Г', 'Д', 'Е', 'Ё', 'Ж', 'З', 'И', 'Й', 'К', 'Л', 'М', 'Н', 'О', 'П', 'Р', 'С', 'Т', 'У', 'Ф', 'Х', 'Ц', 'Ч', 'Ш', 'Щ', 'Ъ', 'Ы', 'Ь', 'Э', 'Ю', 'Я', 'а', 'б', 'в', 'г', 'д', 'е', 'ё', 'ж', 'з', 'и', 'й', 'к', 'л', 'м', 'н', 'о', 'п', 'р', 'с', 'т', 'у', 'ф', 'х', 'ц', 'ч', 'ш', 'щ', 'ъ', 'ы', 'ь', 'э', 'ю', 'я', 'І', 'і', 'Ї', 'ї', 'Є', 'є', ' & ', '&']; $to = ['A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'AE', 'C', 'C', 'C', 'C', 'C', 'D', 'D', 'D', 'E', 'E', 'E', 'E', 'E', 'E', 'E', 'E', 'G', 'G', 'G', 'G', 'G', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'ae', 'c', 'c', 'c', 'c', 'c', 'd', 'd', 'd', 'e', 'e', 'e', 'e', 'e', 'e', 'e', 'e', 'g', 'g', 'g', 'g', 'g', 'H', 'H', 'I', 'I', 'I', 'I', 'I', 'I', 'I', 'I', 'IJ', 'J', 'K', 'L', 'L', 'N', 'N', 'N', 'N', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'CE', 'h', 'h', 'i', 'i', 'i', 'i', 'i', 'i', 'i', 'i', 'ij', 'j', 'k', 'l', 'l', 'n', 'n', 'n', 'n', 'o', 'o', 'o', 'o', 'o', 'o', 'o', 'o', 'o', 'R', 'R', 'S', 'S', 'S', 'S', 'T', 'T', 'T', 'U', 'U', 'U', 'U', 'U', 'U', 'U', 'U', 'U', 'U', 'W', 'Y', 'Y', 'Y', 'Z', 'Z', 'Z', 'r', 'r', 's', 's', 's', 's', 'B', 't', 't', 'b', 'u', 'u', 'u', 'u', 'u', 'u', 'u', 'u', 'u', 'u', 'w', 'y', 'y', 'y', 'z', 'z', 'z', 'A', 'B', 'V', 'H', 'D', 'e', 'Io', 'Z', 'Z', 'Y', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'R', 'S', 'T', 'U', 'F', 'Ch', 'C', 'Ch', 'Sh', 'Shtch', '', 'Y', '', 'E', 'Iu', 'Ia', 'a', 'b', 'v', 'h', 'd', 'e', 'io', 'z', 'z', 'y', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'r', 's', 't', 'u', 'f', 'ch', 'c', 'ch', 'sh', 'shtch', '', 'y', '', 'e', 'iu', 'ia', 'I', 'i', 'Ji', 'ji', 'Je', 'je', '-and-', '-and-']; $identifier = str_replace($from, $to, $identifier); $identifier = mb_strtolower($identifier); $identifier = preg_replace('/[^A-Za-z0-9-]+/', '-', $identifier); $identifier = preg_replace('/[--]+/', '-', $identifier); $identifier = trim($identifier, '-'); $post = $this->_postFactory->create(); $category = $this->_categoryFactory->create(); $number = 1; while (true) { $finalIdentifier = $identifier . ($number > 1 ? '-' . $number : ''); $postId = $post->checkIdentifier($finalIdentifier, $object->getStoreId()); $categoryId = $category->checkIdentifier($finalIdentifier, $object->getStoreId()); if (!$postId && !$categoryId) { break; } else { if ($postId && $postId == $object->getId() && $object instanceof \Magefan\Blog\Model\Post) { break; } if ($categoryId && $categoryId == $object->getId() && $object instanceof \Magefan\Blog\Model\Category) { break; } } $number++; } $object->setData('identifier', $finalIdentifier); }
/** * {@inheritdoc} * * Added custom attributes support. */ public function setData($key, $value = null) { if (is_array($key)) { $key = $this->filterCustomAttributes($key); } else { if ($key == self::CUSTOM_ATTRIBUTES) { $filteredData = $this->filterCustomAttributes([self::CUSTOM_ATTRIBUTES => $value]); $value = $filteredData[self::CUSTOM_ATTRIBUTES]; } } $this->customAttributesChanged = true; parent::setData($key, $value); return $this; }
/** * Perform operations after object load, restore linking with terms and stores * * @SuppressWarnings(PHPMD.CamelCaseMethodName) * * @param \Magento\Framework\Model\AbstractModel $object Thesaurus being loaded * * @return $this */ protected function _afterLoad(\Magento\Framework\Model\AbstractModel $object) { if ($object->getId()) { $stores = $this->getStoreIdsFromThesaurusId($object->getId()); $object->setData('store_id', $stores); $object->setData('stores', $stores); } return parent::_afterLoad($object); }
/** * Using for load sequence profile and setting it into metadata * * @param \Magento\Framework\Model\AbstractModel $object * @return $this */ protected function _afterLoad(\Magento\Framework\Model\AbstractModel $object) { $object->setData('active_profile', $this->resourceProfile->loadActiveProfile($object->getId())); return $this; }
/** * Perform operations after object load * * @param AbstractModel $object * @return $this */ protected function _afterLoad(AbstractModel $object) { if ($object->getId()) { $stores = $this->lookupStoreIds($object->getId()); $object->setData('store_id', $stores); } return parent::_afterLoad($object); }
/** * Lookup for parent_id in already saved transactions of this payment by the order_id * Also serialize additional information, if any * * @param \Magento\Framework\Model\AbstractModel|\Magento\Sales\Model\Order\Payment\Transaction $transaction * @throws \Magento\Framework\Exception\LocalizedException * @return $this */ protected function _beforeSave(\Magento\Framework\Model\AbstractModel $transaction) { $parentTxnId = $transaction->getData('parent_txn_id'); $txnId = $transaction->getData('txn_id'); $orderId = $transaction->getData('order_id'); $paymentId = $transaction->getData('payment_id'); $idFieldName = $this->getIdFieldName(); if ($parentTxnId) { if (!$txnId || !$orderId || !$paymentId) { throw new \Magento\Framework\Exception\LocalizedException(__('We don\'t have enough information to save the parent transaction ID.')); } $parentId = (int) $this->_lookupByTxnId($orderId, $paymentId, $parentTxnId, $idFieldName); if ($parentId) { $transaction->setData('parent_id', $parentId); } } // make sure unique key won't cause trouble if ($transaction->isFailsafe()) { $autoincrementId = (int) $this->_lookupByTxnId($orderId, $paymentId, $txnId, $idFieldName); if ($autoincrementId) { $transaction->setData($idFieldName, $autoincrementId)->isObjectNew(false); } } return parent::_beforeSave($transaction); }
/** * Process page data before saving * * @param AbstractModel $object * @return $this * @throws LocalizedException */ protected function _beforeSave(AbstractModel $object) { /* * For two attributes which represent timestamp data in DB * we should make converting such as: * If they are empty we need to convert them into DB * type NULL so in DB they will be empty and not some default value */ foreach (['custom_theme_from', 'custom_theme_to'] as $field) { $value = !$object->getData($field) ? null : $object->getData($field); $object->setData($field, $this->dateTime->formatDate($value)); } if (!$this->isValidPageIdentifier($object)) { throw new LocalizedException(__('The page URL key contains capital letters or disallowed symbols.')); } if ($this->isNumericPageIdentifier($object)) { throw new LocalizedException(__('The page URL key cannot be made of only numbers.')); } return parent::_beforeSave($object); }