Answer the question
In order to leave comments, you need to log in
Kohana, deleting categories and photos in categories, deleting all photos, how to fix?
Hello, I did the removal by clicking on other examples, the category is deleted and the subcategories are all OK, I added the removal of the photo from the category when you delete the category, so it deletes all the photos of the categories that are in the upload / cat section,
tell me what did you do wrong? I need to delete the category of this category when I delete it, and if it has subcategories and a photo, so that I delete it too.
public function action_delete()
{
$id = (int)$this->request->param('id');
$cat = ORM::factory('category', $id);
if (!$cat->loaded())
{
$this->request->redirect('admin/category');
}
$cat->delete();
//delete sub cats
DB::delete('categories')
->where('parent_id', '=', $id)
->execute();
//delete not existed parent categories
$haveNotExisted = true;
while ($haveNotExisted)
{
$haveNotExisted = false;
$cats = ORM::factory('category')
->find_all();
foreach ($cats as $cat)
{
if ($cat->parent_id != 0)
{
$parent = ORM::factory('category', $cat->parent_id);
if (empty($parent->id))
{
$cat->delete();
$haveNotExisted = true;
}
}
}
}
$directory = DOCROOT . implode(DIRECTORY_SEPARATOR, array('media', 'uploads', 'cat'));
$existed = ORM::factory('category')
->find_all()
->as_array('image_id', 'image_id');
foreach (scandir($directory) as $image)
{
if ($image != '.' && $image != '..' && $image != 'noimage.jpg' && !in_array($image, $existed))
{
@unlink('media/uploads/cat/' . $image);
@unlink('media/uploads/cat/small/small_' . $image);
@unlink('media/uploads/cat/small/admin/admin_' . $image);
}
}
$this->request->redirect('admin/category');
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question