예제 #1
1
 /**
  * Clear useless attribute values
  *
  * @param  \Magento\Framework\Model\AbstractModel $object
  * @return $this
  */
 protected function _clearUselessAttributeValues(\Magento\Framework\Model\AbstractModel $object)
 {
     $origData = $object->getOrigData();
     if ($object->isScopeGlobal() && isset($origData['is_global']) && \Magento\Catalog\Model\ResourceModel\Eav\Attribute::SCOPE_GLOBAL != $origData['is_global']) {
         $attributeStoreIds = array_keys($this->_storeManager->getStores());
         if (!empty($attributeStoreIds)) {
             $delCondition = ['attribute_id = ?' => $object->getId(), 'store_id IN(?)' => $attributeStoreIds];
             $this->getConnection()->delete($object->getBackendTable(), $delCondition);
         }
     }
     return $this;
 }
예제 #2
0
 /**
  * Perform actions after object save
  *
  * @param \Magento\Framework\Model\AbstractModel $object
  * @return $this
  */
 protected function _afterSave(\Magento\Framework\Model\AbstractModel $object)
 {
     if ($object->getGroups()) {
         /* @var $group \Magento\Eav\Model\Entity\Attribute\Group */
         foreach ($object->getGroups() as $group) {
             $group->setAttributeSetId($object->getId());
             if ($group->itemExists() && !$group->getId()) {
                 continue;
             }
             $group->save();
         }
     }
     if ($object->getRemoveGroups()) {
         foreach ($object->getRemoveGroups() as $group) {
             /* @var $group \Magento\Eav\Model\Entity\Attribute\Group */
             $group->delete();
         }
         $this->_attrGroupFactory->create()->updateDefaultGroup($object->getId());
     }
     if ($object->getRemoveAttributes()) {
         foreach ($object->getRemoveAttributes() as $attribute) {
             /* @var $attribute \Magento\Eav\Model\Entity\Attribute */
             $attribute->deleteEntity();
         }
     }
     return parent::_afterSave($object);
 }
예제 #3
0
 /**
  * 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()));
 }
예제 #4
0
파일: Consumer.php 프로젝트: aiesh/magento2
 /**
  * Delete all Nonce entries associated with the consumer
  *
  * @param \Magento\Framework\Model\AbstractModel $object
  * @return $this
  */
 public function _afterDelete(\Magento\Framework\Model\AbstractModel $object)
 {
     $adapter = $this->_getWriteAdapter();
     $adapter->delete($this->getTable('oauth_nonce'), array('consumer_id' => $object->getId()));
     $adapter->delete($this->getTable('oauth_token'), array('consumer_id' => $object->getId()));
     return parent::_afterDelete($object);
 }
예제 #5
0
 /**
  * Check if Entity is Empty
  *
  * @return $this
  * @throws \InvalidArgumentException
  */
 protected function _checkEntityIsEmpty()
 {
     if (!$this->_entity->getId()) {
         throw new \InvalidArgumentException('The model is empty');
     }
     return $this;
 }
예제 #6
0
 /**
  * Delete all Nonce entries associated with the consumer
  *
  * @param \Magento\Framework\Model\AbstractModel $object
  * @return $this
  */
 public function _afterDelete(\Magento\Framework\Model\AbstractModel $object)
 {
     $connection = $this->getConnection();
     $connection->delete($this->getTable('oauth_nonce'), ['consumer_id' => $object->getId()]);
     $connection->delete($this->getTable('oauth_token'), ['consumer_id' => $object->getId()]);
     return parent::_afterDelete($object);
 }
예제 #7
0
 /**
  * Callback for save method in mocked model
  *
  * @throws \Magento\Framework\Exception\LocalizedException
  */
 public function saveModelAndFailOnUpdate()
 {
     if (!$this->_model->getId()) {
         $this->saveModelSuccessfully();
     } else {
         throw new \Magento\Framework\Exception\LocalizedException(__('Synthetic model update failure.'));
     }
 }
 /**
  * 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();
 }
예제 #9
0
 /**
  * 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);
 }
예제 #10
0
 /**
  * Reindex on product delete
  *
  * @param \Magento\Catalog\Model\ResourceModel\Product $productResource
  * @param \Closure $proceed
  * @param \Magento\Framework\Model\AbstractModel $product
  * @return \Magento\Catalog\Model\ResourceModel\Product
  * @SuppressWarnings(PHPMD.UnusedFormalParameter)
  */
 public function aroundDelete(\Magento\Catalog\Model\ResourceModel\Product $productResource, \Closure $proceed, \Magento\Framework\Model\AbstractModel $product)
 {
     $productResource->addCommitCallback(function () use($product) {
         $this->reindexRow($product->getId());
     });
     return $proceed($product);
 }
예제 #11
0
 /**
  * Save relations for Order
  *
  * @param \Magento\Framework\Model\AbstractModel $object
  * @return void
  * @throws \Exception
  */
 public function processRelation(\Magento\Framework\Model\AbstractModel $object)
 {
     /** @var \Magento\Sales\Model\Order $object */
     if (null !== $object->getItems()) {
         /** @var \Magento\Sales\Model\Order\Item $item */
         foreach ($object->getItems() as $item) {
             $item->setOrderId($object->getId());
             $item->setOrder($object);
             $this->orderItemRepository->save($item);
         }
     }
     if (null !== $object->getPayment()) {
         $payment = $object->getPayment();
         $payment->setParentId($object->getId());
         $payment->setOrder($object);
         $this->orderPaymentResource->save($payment);
     }
     if (null !== $object->getStatusHistories()) {
         /** @var \Magento\Sales\Model\Order\Status\History $statusHistory */
         foreach ($object->getStatusHistories() as $statusHistory) {
             $statusHistory->setParentId($object->getId());
             $statusHistory->setOrder($object);
             $this->orderStatusHistoryResource->save($statusHistory);
         }
     }
     if (null !== $object->getRelatedObjects()) {
         foreach ($object->getRelatedObjects() as $relatedObject) {
             $relatedObject->setOrder($object);
             $relatedObject->save();
         }
     }
     $this->addressHandler->removeEmptyAddresses($object);
     $this->addressHandler->process($object);
 }
예제 #12
0
파일: Design.php 프로젝트: aiesh/magento2
 /**
  * Perform actions before object save
  *
  * @param \Magento\Framework\Model\AbstractModel $object
  * @return $this
  * @throws \Magento\Framework\Model\Exception
  */
 public function _beforeSave(\Magento\Framework\Model\AbstractModel $object)
 {
     if ($date = $object->getDateFrom()) {
         $object->setDateFrom($this->dateTime->formatDate($date));
     } else {
         $object->setDateFrom(null);
     }
     if ($date = $object->getDateTo()) {
         $object->setDateTo($this->dateTime->formatDate($date));
     } else {
         $object->setDateTo(null);
     }
     if (!is_null($object->getDateFrom()) && !is_null($object->getDateTo()) && $this->dateTime->toTimestamp($object->getDateFrom()) > $this->dateTime->toTimestamp($object->getDateTo())) {
         throw new \Magento\Framework\Model\Exception(__('Start date cannot be greater than end date.'));
     }
     $check = $this->_checkIntersection($object->getStoreId(), $object->getDateFrom(), $object->getDateTo(), $object->getId());
     if ($check) {
         throw new \Magento\Framework\Model\Exception(__('Your design change for the specified store intersects with another one, please specify another date range.'));
     }
     if ($object->getDateFrom() === null) {
         $object->setDateFrom(new \Zend_Db_Expr('null'));
     }
     if ($object->getDateTo() === null) {
         $object->setDateTo(new \Zend_Db_Expr('null'));
     }
     parent::_beforeSave($object);
 }
예제 #13
0
 /**
  * Perform actions before object save
  *
  * @param \Magento\Framework\Model\AbstractModel $object
  * @return $this
  * @throws \Magento\Framework\Exception\LocalizedException
  */
 public function _beforeSave(\Magento\Framework\Model\AbstractModel $object)
 {
     if ($date = $object->getDateFrom()) {
         $object->setDateFrom($this->dateTime->formatDate($date));
     } else {
         $object->setDateFrom(null);
     }
     if ($date = $object->getDateTo()) {
         $object->setDateTo($this->dateTime->formatDate($date));
     } else {
         $object->setDateTo(null);
     }
     if (!is_null($object->getDateFrom()) && !is_null($object->getDateTo()) && (new \DateTime($object->getDateFrom()))->getTimestamp() > (new \DateTime($object->getDateTo()))->getTimestamp()) {
         throw new \Magento\Framework\Exception\LocalizedException(__('The start date can\'t follow the end date.'));
     }
     $check = $this->_checkIntersection($object->getStoreId(), $object->getDateFrom(), $object->getDateTo(), $object->getId());
     if ($check) {
         throw new \Magento\Framework\Exception\LocalizedException(__('The date range for this design change overlaps another design change for the specified store.'));
     }
     if ($object->getDateFrom() === null) {
         $object->setDateFrom(new \Zend_Db_Expr('null'));
     }
     if ($object->getDateTo() === null) {
         $object->setDateTo(new \Zend_Db_Expr('null'));
     }
     parent::_beforeSave($object);
 }
예제 #14
0
 protected function _beforeSave(\Magento\Framework\Model\AbstractModel $object)
 {
     if (!$object->getId()) {
         $object->setCreatedAt($this->_date->gmtDate());
     }
     $object->setUpdatedAt($this->_date->gmtDate());
     return $this;
 }
예제 #15
0
 /**
  * Method to run after load
  *
  * @param \Magento\Framework\Model\AbstractModel $object
  * @return $this
  */
 protected function _afterLoad(\Magento\Framework\Model\AbstractModel $object)
 {
     $select = $this->getConnection()->select()->from($this->getTable('checkout_agreement_store'), ['store_id'])->where('agreement_id = :agreement_id');
     if ($stores = $this->getConnection()->fetchCol($select, [':agreement_id' => $object->getId()])) {
         $object->setData('store_id', $stores);
     }
     return parent::_afterLoad($object);
 }
예제 #16
0
 protected function _testDelete()
 {
     $modelId = $this->_model->getId();
     $this->_model->delete();
     $model = $this->_getEmptyModel();
     $model->load($modelId);
     \PHPUnit_Framework_Assert::assertEmpty($model->getId(), 'CRUD Delete error');
 }
예제 #17
0
 /**
  * Set attribute value
  *
  * @param \Magento\Framework\Model\AbstractModel $model
  * @return $this
  */
 protected function _setAttributeValue(\Magento\Framework\Model\AbstractModel $model)
 {
     $storeId = $model->getStoreId();
     $defaultStoreId = \Magento\Store\Model\Store::DEFAULT_STORE_ID;
     if (!isset($this->_entityAttributeValues[$model->getId()])) {
         return $this;
     }
     $productValues = $this->_entityAttributeValues[$model->getId()];
     if (!isset($productValues[$storeId]) && !isset($productValues[$defaultStoreId])) {
         return $this;
     }
     $value = isset($productValues[$storeId]) ? $productValues[$storeId] : $productValues[$defaultStoreId];
     $value = $this->_prepareDatetimeValue($value, $model);
     $value = $this->_prepareMultiselectValue($value, $model);
     $model->setData($this->getAttribute(), $value);
     return $this;
 }
예제 #18
0
파일: Website.php 프로젝트: aiesh/magento2
 /**
  * Perform actions after object save
  *
  * @param \Magento\Framework\Model\AbstractModel $object
  * @return $this
  */
 protected function _afterSave(\Magento\Framework\Model\AbstractModel $object)
 {
     if ($object->getIsDefault()) {
         $this->_getWriteAdapter()->update($this->getMainTable(), array('is_default' => 0));
         $where = array('website_id = ?' => $object->getId());
         $this->_getWriteAdapter()->update($this->getMainTable(), array('is_default' => 1), $where);
     }
     return parent::_afterSave($object);
 }
예제 #19
0
 /**
  * Perform actions after object save
  *
  * @param \Magento\Framework\Model\AbstractModel $object
  * @return $this
  */
 protected function _afterSave(\Magento\Framework\Model\AbstractModel $object)
 {
     if ($object->getIsDefault()) {
         $this->getConnection()->update($this->getMainTable(), ['is_default' => 0]);
         $where = ['website_id = ?' => $object->getId()];
         $this->getConnection()->update($this->getMainTable(), ['is_default' => 1], $where);
     }
     return parent::_afterSave($object);
 }
예제 #20
0
 /**
  * Update a "layout update link" if relevant data is provided
  *
  * @param \Magento\Widget\Model\Layout\Update|\Magento\Framework\Model\AbstractModel $object
  * @return $this
  */
 protected function _afterSave(\Magento\Framework\Model\AbstractModel $object)
 {
     $data = $object->getData();
     if (isset($data['store_id']) && isset($data['theme_id'])) {
         $this->getConnection()->insertOnDuplicate($this->getTable('layout_link'), ['store_id' => $data['store_id'], 'theme_id' => $data['theme_id'], 'layout_update_id' => $object->getId(), 'is_temporary' => (int) $object->getIsTemporary()]);
     }
     $this->_cache->clean();
     return parent::_afterSave($object);
 }
예제 #21
0
파일: Group.php 프로젝트: vasuscoin/brand
 /**
  * Process group data before saving
  *
  * @param \Magento\Framework\Model\AbstractModel $object
  * @return $this
  * @throws \Magento\Framework\Exception\LocalizedException
  */
 protected function _beforeSave(\Magento\Framework\Model\AbstractModel $object)
 {
     $connection = $this->getConnection();
     $select = $connection->select()->from($this->getTable('ves_brand_group'), 'url_key')->where('url_key = ?', $object->getUrlKey())->where('group_id != ?', $object->getId());
     $result = $connection->fetchCol($select);
     if (count($result) > 0) {
         throw new \Magento\Framework\Exception\LocalizedException(__('URL key already exists.' . count($result)));
     }
     return parent::_beforeSave($object);
 }
예제 #22
0
 /**
  * Convert array to comma separated value
  *
  * @param \Magento\Framework\Model\AbstractModel $object
  * @return $this
  */
 protected function _beforeSave(\Magento\Framework\Model\AbstractModel $object)
 {
     if (!$object->getId()) {
         $this->_checkUnique($object);
     }
     if (is_array($object->getValue())) {
         $object->setValue(join(',', $object->getValue()));
     }
     return parent::_beforeSave($object);
 }
예제 #23
0
 /**
  * Perform actions after object save
  *
  * @param \Magento\Framework\Model\AbstractModel $object
  * @return \Magento\Framework\Model\Resource\Db\AbstractDb
  */
 protected function _afterSave(\Magento\Framework\Model\AbstractModel $object)
 {
     if ($object->getAttributes()) {
         foreach ($object->getAttributes() as $attribute) {
             $attribute->setAttributeGroupId($object->getId());
             $attribute->save();
         }
     }
     return parent::_afterSave($object);
 }
 public function aroundDelete(\Magento\Catalog\Model\ResourceModel\Category $categoryResource, \Closure $proceed, \Magento\Framework\Model\AbstractModel $category)
 {
     $categoryResource->addCommitCallback(function () use($category) {
         if (!$this->indexer->isScheduled()) {
             Category::$affectedProductIds = (array) $category->getAffectedProductIds();
             $this->indexer->reindexRow($category->getId());
         }
     });
     return $proceed($category);
 }
예제 #25
0
 /**
  * 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;
 }
예제 #26
0
파일: Event.php 프로젝트: aiesh/magento2
 /**
  * Save assigned processes
  *
  * @param \Magento\Framework\Model\AbstractModel $object
  * @return $this
  */
 protected function _afterSave(AbstractModel $object)
 {
     $processIds = $object->getProcessIds();
     if (is_array($processIds)) {
         $processTable = $this->getTable('index_process_event');
         if (empty($processIds)) {
             $this->_getWriteAdapter()->delete($processTable);
         } else {
             foreach ($processIds as $processId => $processStatus) {
                 if (is_null($processStatus) || $processStatus == ProcessModel::EVENT_STATUS_DONE) {
                     $this->_getWriteAdapter()->delete($processTable, array('process_id = ?' => $processId, 'event_id = ?' => $object->getId()));
                     continue;
                 }
                 $data = array('process_id' => $processId, 'event_id' => $object->getId(), 'status' => $processStatus);
                 $this->_getWriteAdapter()->insertOnDuplicate($processTable, $data, array('status'));
             }
         }
     }
     return parent::_afterSave($object);
 }
예제 #27
0
파일: Group.php 프로젝트: nja78/magento2
 /**
  * Method set default group id to the customers collection
  *
  * @param \Magento\Framework\Model\AbstractModel $group
  * @return $this
  */
 protected function _afterDelete(\Magento\Framework\Model\AbstractModel $group)
 {
     $customerCollection = $this->_createCustomersCollection()->addAttributeToFilter('group_id', $group->getId())->load();
     foreach ($customerCollection as $customer) {
         /** @var $customer \Magento\Customer\Model\Customer */
         $customer->load($customer->getId());
         $defaultGroupId = $this->_groupManagement->getDefaultGroup($customer->getStoreId())->getId();
         $customer->setGroupId($defaultGroupId);
         $customer->save();
     }
     return parent::_afterDelete($group);
 }
예제 #28
0
 /**
  * Perform actions before object save
  *
  * @param \Magento\Framework\Model\AbstractModel|\Magento\Framework\Object $object
  * @return $this
  * @throws \Magento\Framework\Exception\LocalizedException
  */
 protected function _beforeSave(\Magento\Framework\Model\AbstractModel $object)
 {
     /** @var \Magento\Sales\Model\Order\Shipment $object */
     if ((!$object->getId() || null !== $object->getItems()) && !count($object->getAllItems())) {
         throw new \Magento\Framework\Exception\LocalizedException(__('We cannot create an empty shipment.'));
     }
     if (!$object->getOrderId() && $object->getOrder()) {
         $object->setOrderId($object->getOrder()->getId());
         $object->setShippingAddressId($object->getOrder()->getShippingAddress()->getId());
     }
     return parent::_beforeSave($object);
 }
예제 #29
0
 /**
  * Change store group website
  *
  * @param \Magento\Framework\Model\AbstractModel $model
  * @return $this
  */
 protected function _changeWebsite(\Magento\Framework\Model\AbstractModel $model)
 {
     if ($model->getOriginalWebsiteId() && $model->getWebsiteId() != $model->getOriginalWebsiteId()) {
         $select = $this->_getWriteAdapter()->select()->from($this->getTable('store_website'), 'default_group_id')->where('website_id = :website_id');
         $groupId = $this->_getWriteAdapter()->fetchOne($select, ['website_id' => $model->getOriginalWebsiteId()]);
         if ($groupId == $model->getId()) {
             $bind = ['default_group_id' => 0];
             $where = ['website_id = ?' => $model->getOriginalWebsiteId()];
             $this->_getWriteAdapter()->update($this->getTable('store_website'), $bind, $where);
         }
     }
     return $this;
 }
예제 #30
-1
 /**
  * @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;
 }