We are moving our forum in GitHub Discussions. For questions about Phalcon v3/v4 you can visit here and for Phalcon v5 here.

Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

Delete entries in link table with a many to many relationship


I can't figure out how to delete my entries from a 'link table'. For example i have a product that can link to multiple categories. I have a table called 'product', 'category' and 'product_in_category'

And i linked all the models with hasmanytomany for the product and category and belongs to for the product_in_category.

To get all these categories we can do: $product->getCategories(); to add multiple categories i can do $product->categories = array(category object, category object)

But how do i delete categories from a product? If I do $product->getCategories()->delete() it looks like it tries to delete the category itself but it should only remove the link between te category and the product.

What is the way to go here? I can't find anything about this in the documentation.

Far from ideal, but could you create a new model for the relationship (ie: that maps to productincategory), then delete instances of that model?


Hi, Also, in Product model you can create a deleteCategories($product_id) function where you will loop through $product->getCategories() resultset


@quasipickle thanks for your reply, this is indeed a working solution. But i was thinking, there is a way to get the linked categories, add the linked categories so maybe there was also be a nice and easy way to delete the linked categories...

But if there is not an option like (at the moment) I will use your solution.

edited Mar '14

@le51 isnt this the same as: $product->getCategories()->delete()? This will result in deleting the 'category' entry and not the 'product in category' entry in my case. I want to remove only the link between the two tables


I think I've posted the wrong answer !

edited Mar '14

If i understand correctly :

model products  
    has many products_in_ categories, foreign key CASCADE  , alias = prodCats
    has many to many categories through products_in_ categories  

model categories
    has many products_in_ categories, foreign key CASCADE, alias catProds
    has many to many products through products_in_ categories

model products_in_categories
    belongs to products
    belongs to categories

product->delete() //should delete the product itself and all products_in_ categories related to specific product, but not related categories
category->delete() //should delete the category itself and all products_in_ categories related to specific category, but not related products

deleting categories from a product is a NO NO,
as the categories might be related to other products that will affect other categories in turn

product->prodCats->delete() // should delete only the links from specific product to any category

markup is not correct, i do not have acces in real code atm, let me know if you need assistance in translating above pseudocode


@humugus, great! This is also an option ofcourse, never thought of this.

But isn't it an idea to have this automatically build in? Or product->categories->delete() should remove the link between the product an the category or some other method like: product->categories->deleteLinks()

Maybe for the future?

But thanks for now!