当前位置: 首页>>代码示例>>PHP>>正文


PHP AbstractAttribute::beforeSave方法代码示例

本文整理汇总了PHP中Magento\Eav\Model\Entity\Attribute\AbstractAttribute::beforeSave方法的典型用法代码示例。如果您正苦于以下问题:PHP AbstractAttribute::beforeSave方法的具体用法?PHP AbstractAttribute::beforeSave怎么用?PHP AbstractAttribute::beforeSave使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Magento\Eav\Model\Entity\Attribute\AbstractAttribute的用法示例。


在下文中一共展示了AbstractAttribute::beforeSave方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。

示例1: beforeSave

 /**
  * Prepare data for save
  *
  * @return $this
  * @throws LocalizedException
  * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  * @SuppressWarnings(PHPMD.NPathComplexity)
  */
 public function beforeSave()
 {
     // prevent overriding product data
     if (isset($this->_data['attribute_code']) && $this->reservedAttributeList->isReservedAttribute($this)) {
         throw new LocalizedException(__('The attribute code \'%1\' is reserved by system. Please try another attribute code', $this->_data['attribute_code']));
     }
     /**
      * Check for maximum attribute_code length
      */
     if (isset($this->_data['attribute_code']) && !\Zend_Validate::is($this->_data['attribute_code'], 'StringLength', ['max' => self::ATTRIBUTE_CODE_MAX_LENGTH])) {
         throw new LocalizedException(__('An attribute code must be fewer than %1 characters.', self::ATTRIBUTE_CODE_MAX_LENGTH));
     }
     $defaultValue = $this->getDefaultValue();
     $hasDefaultValue = (string) $defaultValue != '';
     if ($this->getBackendType() == 'decimal' && $hasDefaultValue) {
         $numberFormatter = new \NumberFormatter($this->_localeResolver->getLocale(), \NumberFormatter::DECIMAL);
         $defaultValue = $numberFormatter->parse($defaultValue);
         if ($defaultValue === false) {
             throw new LocalizedException(__('Invalid default decimal value'));
         }
         $this->setDefaultValue($defaultValue);
     }
     if ($this->getBackendType() == 'datetime') {
         if (!$this->getBackendModel()) {
             $this->setBackendModel('Magento\\Eav\\Model\\Entity\\Attribute\\Backend\\Datetime');
         }
         if (!$this->getFrontendModel()) {
             $this->setFrontendModel('Magento\\Eav\\Model\\Entity\\Attribute\\Frontend\\Datetime');
         }
         // save default date value as timestamp
         if ($hasDefaultValue) {
             $format = $this->_localeDate->getDateFormat(\IntlDateFormatter::SHORT);
             try {
                 $defaultValue = $this->dateTimeFormatter->formatObject(new \DateTime($defaultValue), $format);
                 $this->setDefaultValue($defaultValue);
             } catch (\Exception $e) {
                 throw new LocalizedException(__('Invalid default date'));
             }
         }
     }
     if ($this->getBackendType() == 'gallery') {
         if (!$this->getBackendModel()) {
             $this->setBackendModel('Magento\\Eav\\Model\\Entity\\Attribute\\Backend\\DefaultBackend');
         }
     }
     return parent::beforeSave();
 }
开发者ID:whoople,项目名称:magento2-testing,代码行数:55,代码来源:Attribute.php


注:本文中的Magento\Eav\Model\Entity\Attribute\AbstractAttribute::beforeSave方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。