Magento – How to change the status using mass update function

adminmagento-1.8

Hi all I am able to create a admin grid for my custom module. In this grid I also provide the mass update functionality, and everything is working fine, thanks to some good tutorials on the internet.
The thing is when I use ->setStatus($this->getRequest()->getParam('status'))to set the status for my grid, it is not working, I don't know why ?
so I do it like this ->setStatus('Active') or ->setStatus('Inactive') to change the status for the table values.
I am a new in magento so maybe I didn't understand it properly.
Can anyone tell me why it is not working.
Here is my function in my Grid.php

protected function _prepareMassaction()
{
$this->setMassactionIdField('id');
$this->getMassactionBlock()->setFormFieldName('id');

$this->getMassactionBlock()->addItem('active', array(
'label'=> 'Active',
'url'  => $this->getUrl('*/*/massActive', array('' => ''))
));

$this->getMassactionBlock()->addItem('inactive', array(
'label'=> 'Inactive',
'url'  => $this->getUrl('*/*/massInactive', array('' => ''))
));

$this->getMassactionBlock()->addItem('delete', array(
'label'=> 'Delete',
'url'  => $this->getUrl('*/*/massDelete', array('' => '')),
'confirm' => 'Are you sure?'
));

return $this;
}

and here is my controller function.

public function massActiveAction()
{
$catIds = $this->getRequest()->getParam('id');
if (!is_array($catIds)) {
Mage::getSingleton('adminhtml/session')->addError($this->__('Please select item(s)'));
} else {
try {
foreach ($catIds as $catId) {
Mage::getSingleton('blog/blog')
->load($catId)
->setStatus('Active')
->setIsMassupdate(true)
->save();
}
$this->_getSession()->addSuccess(
$this->__('Total of %d record(s) were successfully updated', count($catIds))
);
} catch (Exception $e) {
$this->_getSession()->addError($e->getMessage());
}
}
$this->_redirect('*/*/index');
}

Best Answer

Grid.php

protected function _prepareMassaction()
{
    $this->setMassactionIdField('id');
    $this->getMassactionBlock()->setFormFieldName('id');

    $this->getMassactionBlock()->addItem('massActive', array(
         'label'    => 'Active',
         'url'      => $this->getUrl('*/*/massActive'),
    ));
    return $this;
}

Controller.php

public function massActiveAction() {
    /*
     * Grid massaction function to activate multiple records from the database
     * */
    $ids = $this->getRequest()->getParam('id');
    if(!is_array($ids)) {
        Mage::getSingleton('adminhtml/session')->addError(Mage::helper('adminhtml')->__('Please select the records you wish to activate'));
    } 
    else {
        try {
            $model = Mage::getModel('blog/blog');
            foreach ($ids as $id) {
                $model->load($id)->setStatus('Active')->save(); 
            }
            Mage::getSingleton('adminhtml/session')->addSuccess(
            Mage::helper('adminhtml')->__('Total of %d record(s) were marked as active.', count($ids)));
        } 
        catch (Exception $e) {
            Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
        }
    }
    $this->_redirect('*/*/');
}
Related Topic