0

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?

5
  • You have not done ->load(SOME_ID) here Commented May 25, 2016 at 13:32
  • I'm fetching a collection which I am later looping through. Don't want to load stuff individually. Commented May 25, 2016 at 13:39
  • Ys, the code you posted should work correctly, but can you try with ->load(ID) and check ? Also do check logs for errors Commented May 25, 2016 at 14:10
  • If I do $model = Mage::getModel('my_custom/custom')->load(1); $model->setStuff('Test'); $model->save(); It does work.. Commented May 25, 2016 at 14:44
  • This only happens when I loop through a collection, if I doStuff() with a normal Mage::getModel('my_custom/custom'), save() works just like normal. Commented May 25, 2016 at 15:27

2 Answers 2

1

I think we've all done this when we first starting out, forget to load the collection object. I've done it myself a few times, forget to load a product so all the attributes and values are loaded and get saved.... However, providing your model, resource model and collection are set up correctly, the primary key comes with the collection. Let's review how to set up models, resource models and collections.

Model:

class My_Custom_Model_Custom extends Mage_Core_Model_Abstract {
    protected function _construct(){
        parent::_construct();
        $this->_init('my_custom/custom');
    }   
}

Resource model:

class My_Custom_Model_Resource_Custom extends Mage_Core_Model_Resource_Db_Abstract {
    protected function _construct(){
        $this->_init('my_custom/custom', 'id');
        $this->_isPkAutoIncrement = true;
    }
}

Collection model:

class My_Custom_Model_Resource_Custom_Collection 
    extends Mage_Core_Model_Resource_Db_Collection_Abstract {
    protected function _construct(){
        parent::_construct();
        $this->_init('my_custom/custom');
    }
}

Make sure you initialize the resource model with the primary key (in this case "id").

0

Problem solved:

Forgot to fetch the PK of the table, therefore save() created a new object with a new PK for each row.

So, don't forget:

->addFieldToSelect('your_id');

Lesson learned: Provide better code example and people will be able to help you.

2
  • great you solved but you didnt explain about FK in qst. also mark this as solved Commented May 26, 2016 at 13:38
  • That's why i wrote: "Lesson learned: Provide better code example and people will be able to help you." That part is on me. Will obviously mark as solved when possible. Commented May 27, 2016 at 8:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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