As a bi-directional, you need to update the association on both sides.
Add this function to the Category object (you can call it addChild if you want):
public function addProduct($product) { $this->children->add($product); }
And then update both associations at the same time:
public function setProductCategory($product_category) { $this->productCategory = $product_category; $product_category->addProduct($this); }
Tip. Do not use $ children / $ parent to describe Entities. Call it that this is $ category / $ product, you will encounter problems when you want to add to another "parent" relationship.
source share