Take the 2-minute tour ×
Magento Stack Exchange is a question and answer site for users of the Magento e-Commerce platform. It's 100% free, no registration required.

I have a custom table. I want to truncate the table using Magento collection without SQL query.

Hope someone will provide some useful information.

share|improve this question
    
Do you mean you want to delete some rows based on some condition? –  Subesh Pokhrel Mar 3 at 12:26
    
No, I need to delete all records and also Auto Increment id start with 1. –  Vijay S Mar 3 at 12:56

2 Answers 2

Magento does not have a support for this (as far as I know).
But you can implement a method in your resource model (not the collection resource model) that will truncate the table.
Something like this:

public function truncate() {
    $this->_getWriteAdapter()->query('TRUNCATE TABLE '.$this->getMainTable());
    return $this;
}

Then you can call it in your code:

Mage::getResourceModel('[module]/[entity]')->truncate();

But this is a very dangerous approach. The truncate statement breaks the transaction, so it cannot be rolled back in case you need to.
I suggest deleteing every entity in the table instead.

$collection = Mage::getModel('[module]/[entity]')->getCollection();
foreach ($collection as $item) {
    $item->delete();
}

The down side of this is that it doesn't reset the increment id of the table. But it's safer.

EDIT.
To update the increment id of the main table you can add a new method in your resource model that does something like this.

public function changeAutoIncrement($increment = 1) {
    $this->_getWriteAdapter()->query('ALTER TABLE '.$this->getMainTable().' AUTO_INCREMENT = '. $increment);
}

Then call your method in the code:

Mage::getResourceModel('[module]/[entity]')->changeAutoIncrement();
share|improve this answer
1  
could you tell me three types of models in magento.thanks in advance –  Meenakshi Sundaram R Mar 3 at 12:42
2  
@MeenakshiSundaramR. There is the entity model. Example: Mage_Catalog_Model_Product. There is the resource model. Example: Mage_Catalog_Model_Resource_Product. And there is the collection resource model Mage_Catalog_Model_Resource_Product_Collection. –  Marius Mar 3 at 12:47
1  
@Marius Can you tell me How to update auto increment id ( its means start with 1 ) available in magento collection. –  Vijay S Mar 3 at 13:03
1  
@VijayS. See my update to the answer. –  Marius Mar 3 at 13:08
    
@Marius Thanks....... –  Vijay S Mar 3 at 13:09

Here's the link if you want to delete rows based on collection if that's what you mean.

http://stackoverflow.com/questions/5486350/how-do-i-delete-data-using-collection-in-magento-orm

share|improve this answer
    
he is asking about truncate the table. Not to delete the record –  Meenakshi Sundaram R Mar 3 at 12:32

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.