Example #1
0
 /**
  * Process category data before saving
  * prepare path and increment children count for parent categories
  *
  * @param \Magento\Framework\DataObject $object
  * @return $this
  * @SuppressWarnings(PHPMD.NPathComplexity)
  * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  */
 protected function _beforeSave(\Magento\Framework\DataObject $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->setParentId($path[$level - 1]);
         }
         if (!$object->getId()) {
             $object->setPath($object->getPath() . '/');
         }
         $this->getConnection()->update($this->getEntityTable(), ['children_count' => new \Zend_Db_Expr('children_count+1')], ['entity_id IN(?)' => $toUpdateChild]);
     }
     return $this;
 }
Example #2
0
 /**
  * Process product data before save
  *
  * @param \Magento\Framework\DataObject $object
  * @return $this
  */
 protected function _beforeSave(\Magento\Framework\DataObject $object)
 {
     /**
      * Check if declared category ids in object data.
      */
     if ($object->hasCategoryIds()) {
         $categoryIds = $this->_catalogCategory->verifyIds($object->getCategoryIds());
         $object->setCategoryIds($categoryIds);
     }
     $self = parent::_beforeSave($object);
     /**
      * Try detect product id by sku if id is not declared
      */
     if (!$object->getId() && $object->getSku()) {
         $object->setId($this->getIdBySku($object->getSku()));
     }
     return $self;
 }