#1151 Delete sub categories when deleting a parent category #465

This commit is contained in:
Diatrex 2020-03-30 13:50:49 +03:00
parent 76c38c7f9b
commit a9b811dc44
3 changed files with 4 additions and 38 deletions

View File

@ -24,7 +24,7 @@ class CategoryModel extends CatsCategoryEntryModel implements CategoryInterface
$cat_ids[] = $cat->id;
$subCat = $cat->parent_category_id;
if ($subCat != null) {
for ($i = 0; $i < 7; $i++) {
for ($i = 0; $i < 10; $i++) {
$parCat = $this->getCat($subCat);
if (isset($parCat)) {
if ($parCat->parent_category_id == "") {
@ -83,9 +83,8 @@ class CategoryModel extends CatsCategoryEntryModel implements CategoryInterface
public function deleteSubCategories($id)
{
$subCategories = $this->getAllSubCategories($id);
foreach ($subCategories as $subCategory) {
$this->find($subCategory)->delete();
}
$this->newQuery()->whereIn('id', $subCategories)->delete();
return true;
}

View File

@ -68,37 +68,6 @@ class CategoryRepository extends EntryRepository implements CategoryRepositoryIn
{
$this->model->find($id)->delete();
$this->deleteSubcategories($id);
}
public function deleteSubcategories($id)
{
// Get all subcategories
$allSubcategories = array();
$subcategories = $this->getSubCatById($id);
if (count($subcategories)) {
foreach ($subcategories as $subcategory) {
$allSubcategories[$subcategory->id] = ['id' => $subcategory->id, 'processed' => false];
}
do {
$unprocessedCategories = array_filter($allSubcategories, function ($unprocessedCategory) {
return $unprocessedCategory['processed'] === false;
});
foreach ($unprocessedCategories as $unprocessedCategory) {
$subcategories = $this->getSubCatById($unprocessedCategory['id']);
foreach ($subcategories as $subcategory) {
$allSubcategories[$subcategory->id] = ['id' => $subcategory->id, 'processed' => false];
}
$allSubcategories[$unprocessedCategory['id']]['processed'] = true;
}
} while (count($unprocessedCategories));
// Delete all subcategories
$whereIn = array();
foreach ($allSubcategories as $category) {
$whereIn[] = $category['id'];
}
$this->newQuery()->whereIn('id', $whereIn)->delete();
}
$this->model->deleteSubCategories($id);
}
}

View File

@ -21,6 +21,4 @@ interface CategoryRepositoryInterface extends EntryRepositoryInterface
public function getCategories();
public function DeleteCategories($id);
public function deleteSubcategories($id);
}