0

I searched for a way to clean rows in DB, where something, through a resource collection call, but could not find relevant information.

I.e., I have table with fields store_id , path , value and 400 rows and I want to clean the rows where the store_id is 1 (or something else). My question is - is this possible through a collection call, or with the resource model?

The table is custom, and I have resource model for it.

0

It is possible.

$collection = Mage::getModel('[namespace]/[module]')->getCollection()
    ->addFieldToFilter('store_id', 1);

foreach ($collection as $item) {
    $item->delete();
}

[EDIT]
An alternative is to add this method in your resource model (not collection model, just resource).

public function deleteByStoreId($storeId) {
    $adapter = $this->_getWriteAdapter();
    $condition = array(
        'store_id = ?'     => $storeId,
    );
    $adapter->delete($this->getMainTable(), $condition);
    return $this;
}

and then just call

Mage::getResourceModel('[namespace]/[module]')->deleteByStoreId();

of course this is just an example. You can make a general delete method so you can pass as many filters as you want.

  • Yeah, but this might take time if there are more entries. Isn't there a way to delete multiple entries at one time. Something like $model = Mage::getModel('[module]/[model]')->getResource()->addFieldToFilter($scopeId); and then $model->delete(); – Syspect May 22 '14 at 13:26
  • 1
    @Syspect. See my update. Maybe that helps. – Marius May 22 '14 at 13:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.