I've created a custom model in my custom module. See dummy code below. When i loop through a collection and call save on my object, it creates a NEW object in the database instead of saving on the old object. Why does this happen?
$myCustomCollection = Mage::getModel('my_custom/custom')
->getCollection()
->addFieldToSelect('*'); //Just dummy code
if($myCustomCollection !== 0) {
foreach($myCustomCollection as $custom)
{
$something = $custom->getSomething();
switch($something)
{
case "something":
$this->doStuff($custom);
break;
default:
$this->doOtherStuff($custom);
break;
}
}
}
public function doStuff($custom) {
$custom->setAnotherThing('Hey');
$custom->save();
//CALLING SAVE() CREATES A NEW OBJECT IN THE DB,
//IT DOES NOT UPDATE THE OLD ONE..WHY?
}
If i create a random testfile and do a Mage::getModel('my_custom/custom')->load(1), update a random thing and then use save(), it updates the current model, so here it works fine. Any ideas?
->load(SOME_ID)
here – Anil Suthar May 25 '16 at 13:32->load(ID)
and check ? Also do check logs for errors – Anil Suthar May 25 '16 at 14:10$model = Mage::getModel('my_custom/custom')->load(1); $model->setStuff('Test'); $model->save();
It does work.. – user1749754 May 25 '16 at 14:44