0

I am facing an issue with deleting rows from table by custom field.

Below my database table

link_id | quote_id  | giftcard_value | giftcard | redeemed_at
--------------------------------------------------------------
10000   | 20000     | 450            | XSERTTY3 | 2015-12-01
10001   | 20000     | 600            | DSFDTTY3 | 2015-12-01

I need to delete all the entries by quote_is.

My code is like this

Mage::getModel('xxxxxxxxxx/xxxxxxxxxx')->load($quote->getId(),'quote_id')->delete();

But its working for one entry, but not working for may entries. Please help me

3

No need to load the entire entities it is not optimal in terms of performance.

You should use collections instead:

$collection = Mage::getResourceModel('xxxxxxxxxx/xxxxxxxxxx_collection')->addFieldToFilter('quote_id', $quote->getId());
foreach ($collection as $item) {
    $item->delete();
}
  • Its giving PHP Fatal error: Call to undefined method xxxx_xxxx_Model_Resource_xxxxx_Collection::delete() – Rinto George Jul 27 '16 at 13:08
  • @duke see my update – Raphael at Digital Pianism Jul 27 '16 at 13:21
3

Here ->load($quote->getId(),'quote_id') may return multiple rows, so if you do ->delete() on this collection, ONLY first row will be deleted.

You should loop through the collection for deleting all fetched rows :

$items = Mage::getModel('xxxxxxxxxx/xxxxxxxxxx')->load($quote->getId(),'quote_id');
foreach($items as $item){
    $item->delete();
}

Otherwise, I have heared about deleteAll method (BUT I've never tried it) :

//Not sure, but Give it a try
Mage::getModel('xxxxxxxxxx/xxxxxxxxxx')->load($quote->getId(),'quote_id')->deleteAll();

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.