Last active
March 12, 2019 19:46
-
-
Save mklooss/4528773 to your computer and use it in GitHub Desktop.
Magento - Delete in Collection in one request
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
It works!! Thanks 👍