0

I am a newbie in magento development and want to implement softdelete on my models . So basically I have added a is_deleted field in tables. So when I delete any record I just set is_deleted to 1. Now my question is that if I select record(s) from either model or resource Model only filtered record where is_deleted is 0 should come. For Exmaple - $model->load($id) should return the record which is not soft deleted. Thanks for the help in advance

2 Answers 2

0

In your model you would need to override Mage_Core_Model_Abstract::load() function.

If the soft delete is on all models you need to override: Mage_Core_Model_Resource_Db_Abstract::load() method to something like:

public function load(Mage_Core_Model_Abstract $object, $value, $field = null)
{
    if (is_null($field)) {
        $field = $this->getIdFieldName();
    }

    $read = $this->_getReadAdapter();
    if ($read && !is_null($value)) {
        $select = $this->_getLoadSelect($field, $value, $object);
        $data = $read->fetchRow($select);

        if ($data && $data[is_delete] != 1) {
            $object->setData($data);
        }
    }

    $this->unserializeFields($object);
    $this->_afterLoad($object);

    return $this;
}

Here is how you could override an abstract class in Magento: https://stackoverflow.com/questions/15739628/override-extend-magento-abstract-class

1
  • Hi Paras, Thanks for the help I was looking for same code. Mar 20, 2015 at 11:54
2
$model = Mage::getModel('modulename/modelname')->getCollection()
         ->addFieldToFilter('is_deleted',0);

this code give a query like this

select * from yourtablename where (is_deleted= '0')

This $model will return only that data where is_deleted = 0

5
  • Hi Thanks for help but I was looking for a generalized way where it may be applied to all models Mar 20, 2015 at 11:55
  • Just A little question that What should I do If I want to display the items in grid without applying AddFieldtoFilter using the load function logic ? Mar 25, 2015 at 11:30
  • you want to get data using load($id) ri8? if yes than it will return only single row data please clarify what you want and using AddFieldtoFilter what's your problem
    – ND17
    Mar 26, 2015 at 5:24
  • 1
    I just want to implement softdelete in models so when I get result set either using load or in grid view is should be filtered by is_deleted coloum. Mar 27, 2015 at 5:49
  • have you any unique id? and using that id you can get data which is filtered by is_deleted and in grid view you want data filtered by is_deleted coloum than write code in _prepareCollection()
    – ND17
    Mar 27, 2015 at 6:17

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.