I'm implementing a user interface on the backend for adding an arbitrary number of values to a product - just like the price tier interface. I'd like to know how saving data like this is usually done.
Below is some simplified code of my attempt (which throws this error: Item with the same id "1" already exists"):
$myItemCollection = $product->getMyItemCollection();
if(!$myItemCollection) {
$myItemCollection = Mage::getModel('my_module/my_item')->getCollection();
}
foreach($product->getMyData('items') as $data) {
$myItem = Mage::getModel('my_module/my_item')->addData($data);
// Item with the same id "1" already exist on the second iteration
$myItemCollection->addItem($myItem);
}
$myItemCollection
->setDataToAll('product_id', $product->getId())
->save();
$product->getMyData('items') returns something like:
array(
1 => array(
'foo' => 'bar'
),
2 => array(
'bin' => 'baz'
),
3 => array(
'buz' => 'fuz'
)
)