Home is for the experience. Projects are for the ones who are interested!

How To Delete From Multiple Tables In Prestashop

How To Delete From Multiple Tables In Prestashop

In this tutorial I will show you how do delete from multiple tables in prestashop using joins. Sometimes you may need to delete data which lies in more than one table so you will have to join in all tables where your information is and then delete it. If you used join in your selects in prestashop is almost the same thing you will need to change only few things.

First of all we need to create our sql, for instance let's say we need to delete a post from one table and its comments from the second table. Our code should look like the following:

$sql = 'DELETE post, comment FROM '._DB_PREFIX_.'posts post 
  JOIN '._DB_PREFIX_.'comments comment ON (comment.id_post = '.$id_post.') WHERE post.id = '.$id_post;

With this sql we first check in the posts table by post id, then we go in the comments table where we search for all comments which have post id equal with our post id. To run the above sql we need to add one more line of code like the one below:

$status = DB::getInstance()->execute($sql);

In the $status variable will be stored true if no error will come back and false if something went wrong. In this way you can delete informations which lies in multiple table in prestashop. Let me know if you have any questions in the comment section and I will try to help you.

Leave a Reply

Your email address will not be published. Required fields are marked *