Skip to content

Instantly share code, notes, and snippets.

@mklooss
Last active March 12, 2019 19:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mklooss/4528773 to your computer and use it in GitHub Desktop.
Save mklooss/4528773 to your computer and use it in GitHub Desktop.
Magento - Delete in Collection in one request
<?php
class Something_Asdf_Model_Resource_Halma_Collection
extends Mage_Core_Model_Resource_Db_Collection_Abstract
{
/**
* delete all the entities in the collection
*
* @return $this
*/
public function delete()
{
$id = array();
foreach ($this->getItems() as $item) {
$id[] = $item->getId();
}
if(empty($id))
{
return $this;
}
$quoted = $this->_getWriteAdapter()->quoteInto('IN (?)', $id);
$this->_getWriteAdapter()->delete($this->getMainTable(), "id {$quoted}");
return $this;
}
/**
*
* @return type
*/
protected function _getWriteAdapter()
{
if(is_null($this->_write))
{
$this->_write = Mage::getSingleton('core/resource')->getConnection('core_write');
}
return $this->_write;
}
@jruzafa
Copy link

jruzafa commented Mar 23, 2017

It works!! Thanks 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment