Hi, I want an update of the collection of a table magento, I have a table model and I want to add a value in a row in foreach, but it doesn't work. This is the code:
$wishes = $this->getCollection()->addFieldToSelect('*');
$wishes->addFieldToFilter('id_product',$id_product);
$wishes->getSelect()->columns('MAX(num_wishes) as num_wish')->columns('COUNT(*) as total_wishes')->group('num_wishes')->group('id_user');
$wishes->setOrder('num_wish', 'desc');
$a_ids_user = array();
$percent_cost_product = Mage::getModel('core/variable')->loadByCode('gem-percentage')->getValue('int');
foreach( $wishes->getData() as $wish ){
if(!$this->existIdUser($a_ids_user, $wish['id_user'])){
array_push($a_ids_user, $wish['id_user']);
Mage::log("set GEM " . print_r($wish,true), null, 'wishcompartia_setup.log', true);
$wish->setData('gemas', 20 );
$wish->save();
}
}
Thnks
Solved! Go to Solution.
You need to to setId and addData for update and load model
$wishes = $this->getCollection()->addFieldToSelect('*'); $wishes->addFieldToFilter('id_product',$id_product); $wishes->getSelect()->columns('MAX(num_wishes) as num_wish')->columns('COUNT(*) as total_wishes')->group('num_wishes')->group('id_user'); $wishes->setOrder('num_wish', 'desc'); $a_ids_user = array(); $percent_cost_product = Mage::getModel('core/variable')->loadByCode('gem-percentage')->getValue('int'); foreach( $wishes->getData() as $wish ){ if(!$this->existIdUser($a_ids_user, $wish['id_user'])){ array_push($a_ids_user, $wish['id_user']); Mage::log("set GEM " . print_r($wish,true), null, 'wishcompartia_setup.log', true);
$wishmodel=Mage::getModel('modelnode/modelname');
$wishmodel->setId($wish->getId());
$wishmodel->addData('gemas', 20 );
$wishmodel->save(); } }
@aperezcompartiakindly mark as accepted solution so it can help future reader too
You need to to setId and addData for update and load model
$wishes = $this->getCollection()->addFieldToSelect('*'); $wishes->addFieldToFilter('id_product',$id_product); $wishes->getSelect()->columns('MAX(num_wishes) as num_wish')->columns('COUNT(*) as total_wishes')->group('num_wishes')->group('id_user'); $wishes->setOrder('num_wish', 'desc'); $a_ids_user = array(); $percent_cost_product = Mage::getModel('core/variable')->loadByCode('gem-percentage')->getValue('int'); foreach( $wishes->getData() as $wish ){ if(!$this->existIdUser($a_ids_user, $wish['id_user'])){ array_push($a_ids_user, $wish['id_user']); Mage::log("set GEM " . print_r($wish,true), null, 'wishcompartia_setup.log', true);
$wishmodel=Mage::getModel('modelnode/modelname');
$wishmodel->setId($wish->getId());
$wishmodel->addData('gemas', 20 );
$wishmodel->save(); } }
Thnks man, it works
@aperezcompartiakindly mark as accepted solution so it can help future reader too