Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do i delete data using collection in magento ORM?

Tags:

magento

Right now i am deleting data like

  $deleteCCL = Mage::getModel('crossdata/customccitem');
  $deleteCCL->load($itemId);
  $deleteCCL->delete();

Is there any way to delete data using collection like:

$rcc = Mage::getModel('crossdata/customccitem')->getCollection()->delete();

?

Thanks a lot,

Balan

like image 909
balanv Avatar asked Mar 30 '11 12:03

balanv


2 Answers

There isn't a handy group delete function so either add it to your collection or simply do it directly.

foreach ($rcc as $ccitem) {
    $ccitem->delete();
}
like image 69
clockworkgeek Avatar answered Sep 29 '22 05:09

clockworkgeek


Mage_Eav_Model_Entity_Collection_Abstract (which extends Varien_Data_Collection_Db) provides a delete() method for collections if you have the ability to extend it.

However, it's implementation is basically the same as yours:

/**
 * Delete all the entities in the collection
 *
 * @todo make batch delete directly from collection
 */
public function delete()
{
    foreach ($this->getItems() as $k=>$item) {
        $this->getEntity()->delete($item);
        unset($this->_items[$k]);
    }
    return $this;
}
like image 45
leek Avatar answered Sep 29 '22 05:09

leek