A
A
Anton2015-10-21 17:22:49
Kohana
Anton, 2015-10-21 17:22:49

Deleting a product, how can I make sure that when deleting a product, the photo of the product is deleted?

Hello , how to implement the complete deletion of the product, at the moment after the product the photo remains on the server, how to make sure that when deleting the product, all the photos that are in the product are deleted, deleted from the hosting.
Site on Kohana 3.1
Action for deleting a product:

public function action_delete()
{
$id = (int)$this->request->param('id');
$products = ORM::factory('product', $id);
$products->delete();
$this->request->redirect('admin/products');
}

In the product, each photo has a delete button, but also we will not delete each photo separately, and then the product, I’m talking about what is implemented there when removing a photo from a product, it is deleted from the hosting.
Maybe you can somehow combine the code so that when you delete the product, all the photos that are in the product are deleted.
public function action_delimg()
    {
        $id = (int)$this->request->param('id');
        $images = ORM::factory('image', $id);
        $product_id = $images->product_id;

        if (!$images->loaded()) {
            $this->request->redirect('admin/products');
        }

        $p_db = ORM::factory('product', $product_id);
        if ($p_db->image_id == $id) {
            $p_db->image_id = 0;
            $p_db->save();
        }

        @unlink('media/uploads/' . $images->image);
        @unlink('media/uploads/small/small_' . $images->image);
        $images->delete();
        $this->request->redirect('admin/products/edit/' . $product_id);
    }

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Sergey, 2015-10-21
@gangstarcj

get an array of photos and delete them
Can the photo be used in another product?

H
holfza, 2015-10-21
@holfza

Why not make it in a table with pictures?
ON DELETE CASCADE

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question