Example #1
0
 /**
  * @param array $item
  * @param int $parentId
  * @return int
  */
 protected function createCategory($item, $parentId)
 {
     if (empty($item) || empty($item[static::ELEMENT_ID]) || empty($item[static::ELEMENT_NAIMENOVANIE])) {
         return $parentId;
     }
     $result = $parentId;
     if (isset($this->categoryCache[$item[static::ELEMENT_ID]])) {
         $result = $this->categoryCache[$item[static::ELEMENT_ID]];
     } else {
         $guid = CommercemlGuid::findOne(['guid' => $item[static::ELEMENT_ID]]);
         if (empty($guid)) {
             $guid = new CommercemlGuid();
             $guid->guid = $item[static::ELEMENT_ID];
             $guid->name = $item[static::ELEMENT_NAIMENOVANIE];
             $guid->type = 'CATEGORY';
             $guid->model_id = 1;
             $category = Category::findOne(['slug' => Helper::createSlug($item[static::ELEMENT_NAIMENOVANIE]), 'parent_id' => $parentId]);
             if (empty($category)) {
                 if (null !== ($category = Category::createEmptyCategory($parentId, null, $item[static::ELEMENT_NAIMENOVANIE]))) {
                     $guid->model_id = $category->id;
                 }
             } else {
                 $guid->model_id = $category->id;
             }
             $guid->save();
             $guid->refresh();
             $result = $this->categoryCache[$item[static::ELEMENT_ID]] = $guid->model_id;
         } else {
             $result = $this->categoryCache[$item[static::ELEMENT_ID]] = $guid->model_id;
         }
     }
     return $result;
 }