开发者

How do i delete data using collection in magento ORM?

开发者 https://www.devze.com 2023-02-21 07:48 出处:网络
Right now i am deleting data like $deleteCCL = Mage::getModel(\'crossdata/customccitem\'); $deleteCCL->load($itemId);

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


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();
}


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;
}


To implement delete functionality into a collection you have to add a new method to the collection class or a custom abstract class the collection inherits from.

Example:

public function delete()
{
    foreach ($this->getItems() as $key => $item) {
        $item->delete();
        unset($this->_items[$key]);
    }

    return $this;
}


Thanks to @leek, I got mine working:

foreach( $collection as $item )
    if( <<logic>> ) $collection->getEntity()->delete( $item );


You can use collection walk function

Mage::getModel('crossdata/customccitem')->getCollection()->walk('delete');

or each function

Mage::getModel('crossdata/customccitem')->getCollection()->each('delete')->clear();
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号