// Find parent for newly created category
$params['parentId'
] =
isset($params['parentId'
]) &&
is_numeric($params['parentId'
]) ?
(int) $params['parentId'
] : 1;
/** @var Category $parentCategory */
$parentCategory =
$repo->
find($params['parentId'
]);
$categoryModel->
setParent($parentCategory);
// If Leaf-Category gets child category move all assignments to new child category
if ($parentCategory->
getChildren()->
count() === 0 &&
$parentCategory->
getArticles()->
count() > 0
) { /** @var Article $product */
foreach ($parentCategory->
getArticles() as $product) { $product->
removeCategory($parentCategory);
$product->
addCategory($categoryModel);
} } } else { $categoryModel =
$repo->
find($categoryId);
} // check if a category could be found
if (!
$categoryModel) { $this->
View()->
assign([ 'success' => false,