VijayS91
VijayS91

Reputation: 1531

How to truncate magento table using collection

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

Hope someone will provide some useful information

Upvotes: 5

Views: 3589

Answers (2)

user2930516
user2930516

Reputation: 139

I know it's a bit late but :

  $tableName = $collection->getResource()->getMainTable();
  $conn = $collection->getConnection();
  $conn->truncateTable($tableName);

Upvotes: 3

klipach
klipach

Reputation: 816

Here is a small example for cron/schedule table:

require_once('./app/Mage.php');

Mage::app("default");

/** @var Mage_Cron_Model_Resource_Schedule $cron */
$cron = Mage::getResourceModel('cron/schedule');

$cron->getReadConnection()->delete(
    $cron->getMainTable(),
    '1=1'
);

If you extend Mage_Core_Model_Resource_Db_Abstract in your module this example will work and you should use getWriteConnection instead of getReadConnection.

Upvotes: 0

Related Questions