/** * Perform actions before object save * * @param \Magento\Framework\Model\AbstractModel $object * @return $this */ protected function _beforeSave(\Magento\Framework\Model\AbstractModel $object) { $belongsToQuoteItem = $object->hasData(CommentInterface::QUOTE_ITEM_ID_COLUMN); $belongsToOrderItem = $object->hasData(CommentInterface::ORDER_ITEM_ID_COLUMN); $hasText = $object->hasData(CommentInterface::TEXT_COLUMN); // A comment should either be attached to a quote item or order item... if (!$belongsToQuoteItem || !$belongsToOrderItem) { throw new \Magento\Framework\Exception\LocalizedException(__('A comment should belong to either a quote item or order item.')); } // ...but not to both if ($belongsToQuoteItem && $belongsToOrderItem) { throw new \Magento\Framework\Exception\LocalizedException(__('A comment cannot belong to both a quote item and order item.')); } if (!$hasText) { throw new \Magento\Framework\Exception\LocalizedException(__('A comment requirescomp text.')); } return parent::_beforeSave($object); }
/** * Validate product attribute value for condition * * @param \Magento\Framework\Model\AbstractModel $model * @return bool */ public function validate(\Magento\Framework\Model\AbstractModel $model) { $attrCode = $this->getAttribute(); if ('category_ids' == $attrCode) { return $this->validateAttribute($model->getAvailableInCategories()); } $oldAttrValue = $model->hasData($attrCode) ? $model->getData($attrCode) : null; $this->_setAttributeValue($model); $result = $this->validateAttribute($model->getData($this->getAttribute())); $this->_restoreOldAttrValue($model, $oldAttrValue); return (bool) $result; }
/** * {@inheritdoc} */ protected function _beforeSave(AbstractModel $object) { $reindexRequired = false; if ($object->hasData('attributes') && is_array($object->getData('attributes'))) { $serialized = serialize($object->getData('attributes')); if ($object->getData('attributes_serialized') != $serialized) { $reindexRequired = true; } $object->setData('attributes_serialized', $serialized); } if ($object->hasData('properties') && is_array($object->getData('properties'))) { $serialized = serialize($object->getData('properties')); if ($object->getData('properties_serialized') != $serialized) { $reindexRequired = true; } $object->setData('properties_serialized', $serialized); } if ($reindexRequired && !$object->dataHasChangedFor('status')) { $object->setStatus(Config::INDEX_STATUS_INVALID); } $object->setCode($object->getCode()); return $this; }
/** * Perform actions before object save * * @param AbstractModel $object * @return $this */ protected function _beforeSave(AbstractModel $object) { if (!$object->getId()) { $object->setCreatedAt($this->_date->gmtDate()); } if ($object->hasData('stores') && is_array($object->getStores())) { $stores = $object->getStores(); $stores[] = 0; $object->setStores($stores); } elseif ($object->hasData('stores')) { $object->setStores([$object->getStores(), 0]); } return $this; }
/** * @param \Magento\Framework\Model\AbstractModel $model * @return bool */ public function validate(\Magento\Framework\Model\AbstractModel $model) { if (!$model->hasData($this->getAttribute())) { $model->load($model->getId()); } $attributeValue = $model->getData($this->getAttribute()); return $this->validateAttribute($attributeValue); }