cancel
Showing results for 
Search instead for 
Did you mean: 

Update groupBy collection model

SOLVED

Update groupBy collection model

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

2 ACCEPTED SOLUTIONS

Accepted Solutions

Re: Update groupBy collection model

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(); } }

 

Find helpful ? Consider Giving Kudos to this post.
Problem solved? Click Accept as Solution!"
Qaisar Satti

View solution in original post

Re: Update groupBy collection model

@aperezcompartiakindly mark as accepted solution so it can help  future reader too

Find helpful ? Consider Giving Kudos to this post.
Problem solved? Click Accept as Solution!"
Qaisar Satti

View solution in original post

3 REPLIES 3

Re: Update groupBy collection model

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(); } }

 

Find helpful ? Consider Giving Kudos to this post.
Problem solved? Click Accept as Solution!"
Qaisar Satti

Re: Update groupBy collection model

Thnks man, it works

Re: Update groupBy collection model

@aperezcompartiakindly mark as accepted solution so it can help  future reader too

Find helpful ? Consider Giving Kudos to this post.
Problem solved? Click Accept as Solution!"
Qaisar Satti