When a product is assigned to a category, Its relation is save is catalog_category_product table.

But on searching for setPostedProducts() it seems like a magic method. So how the data is saved in catalog_category_product table.


as you suspected setPostedProducts is a magic method.
It just does $category->setData('posted_product', $products).
The values set are later retrieved using getPostedProducts when the category is saved.

Here is the backtrace.

  • Mage_Catalog_Model_Category::save(); calls Mage_Catalog_Model_Resource_Category::save
  • ...that calls Mage_Catalog_Model_Resource_Category::_afterSave
  • ...that calls Mage_Catalog_Model_Resource_Category::_saveCategoryProducts
  • that calls getPostedProducts and does something with the results.

See the method _saveCategoryProducts, specially the part starting with $products = $category->getPostedProducts();

  • Thanks Marius, Now I understood how data is saved in catalog_category_product table. – Trimantra Software Solution Jun 12 '15 at 8:51
  • does it work on others? i mean, could i use setPostedSlide or something? – fudu Aug 10 '18 at 9:30
  • the setter works if your class extends Varien_Object. $object->setPostedSlide() is equivalent to $object->setData('posted_slide'). But it will not auto-magically insert values in your db. You need your custom logic that gets the values you previously set and persists them in the db. – Marius Aug 10 '18 at 10:57
  • What I found : \Magento\Catalog\Model\CategoryLinkManagement::assignProductToCategories / \Magento\Catalog\Model\CategoryLinkRepository::save / \Magento\Catalog\Model\ResourceModel\Category::_saveCategoryProducts – Mac A. Feb 6 '20 at 23:46

I guess it is important to add to Marius' answer that $products is an array where key is a product ID (oh, PHP) and value is position of product in category.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.