I am getting an error while trying to pragmatically remove products from some categories.
Error:
Fatal error: Uncaught TypeError: Argument 3 passed to Mage_Catalog_Model_Resource_Abstract::_canUpdateAttribute() must be of the type array, null given, called in /xxx/public_html/app/code/core/Mage/Eav/Model/Entity/Abstract.php on line 1225 and defined in /xxx/public_html/app/code/core/Mage/Catalog/Model/Resource/Abstract.php:540
Stack trace: #0 /xxx/public_html/app/code/core/Mage/Eav/Model/Entity/Abstract.php(1225): Mage_Catalog_Model_Resource_Abstract->_canUpdateAttribute(Object(Mage_Catalog_Model_Resource_Eav_Attribute), '2019-01-14 00:0...', NULL)
#1 /xxx/public_html/app/code/core/Mage/Eav/Model/Entity/Abstract.php(1123): Mage_Eav_Model_Entity_Abstract->_collectSaveData(Object(Mage_Catalog_Model_Product))
#2 /xxx/public_html/app/code/core/Mage/Core/Model/Abstract.php(318): Mage_Eav_Model_Entity_Abstract->save(Object(Mage_Catalog_Model_Product))
#3 /home/admin/domains/gr in /xxx/public_html/app/code/core/Mage/Catalog/Model/Resource/Abstract.php on line 540
And my code:
$products = Mage::getModel('catalog/category')->setStoreId($storeId)->load($specialCategoryId)
->getProductCollection()
->addAttributeToSelect(array('special_to_date', 'category_ids'))
->addAttributeToFilter('special_to_date', array('lteq' => date("Y-m-d")));
foreach ($products as $product) {
$oldCategories = $product->getCategoryIds();
$newCategories = array_diff($oldCategories, $specialCategoryIds);
if($oldCategories != $newCategories){
$_product = Mage::getModel('catalog/product')->load($product->getEntityId());
$_product->setCategoryIds($newCategories);
Mage::log('Product ID: ' .$product->getEntityId(), null, 'RemoveFromCategoryLog.log', true);
try {
$_product->save();
} catch(Exception $e){
Mage::log($e->getMessage(), null, 'RemoveFromCategoryLog.log', true);
}
}
}
Loading the product did not solve it. And as there are lots of products i am reluctant to use category_api.