/** * @param string $categorypaths * @return array */ public function createCategoriesByCategoryPaths($categorypaths) { $categoryIds = array(); $categorypaths = explode("\n", $categorypaths); foreach ($categorypaths as $categorypath) { $categorypath = trim($categorypath); if (empty($categorypath)) { continue; } $categories = explode('|', $categorypath); $categoryId = 1; foreach ($categories as $categoryName) { $categoryName = trim($categoryName); if (empty($categoryName)) { break; } $categoryModel = $this->getCategoryRepository()->findOneBy(array('name' => $categoryName, 'parentId' => $categoryId)); if (!$categoryModel) { $parent = $this->getCategoryRepository()->find($categoryId); if (!$parent) { throw new \Exception(sprintf('Could not find %s ')); } $categoryModel = new \Shopware\Models\Category\Category(); $categoryModel->setParent($parent); $categoryModel->setName($categoryName); $this->getManager()->persist($categoryModel); $this->getManager()->flush(); $this->getManager()->clear(); } $categoryId = $categoryModel->getId(); if (empty($categoryId)) { continue; } if (!in_array($categoryId, $categoryIds)) { $categoryIds[] = $categoryId; } } } return $categoryIds; }