I have a custom table. I want to truncate the table using Magento collection without SQL query.
Hope someone will provide some useful information.
I have a custom table. I want to truncate the table using Magento collection without SQL query.
Hope someone will provide some useful information.
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();
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
.
$model = Mage::getModel('[module]/[model]');
$resource = $model->getResource();
$connection = $resource->getReadConnection();
/* @see Varien_Db_Adapter_Pdo_Mysql */
$connection->truncateTable($resource->getMainTable());
$connection->changeTableAutoIncrement($resource->getMainTable(), 1);
Note: MySQL resets autoincrement value to 1
when truncate a table, so it isn't required, but changeTableAutoIncrement
may be useful if someone want to have AI starting with another value.
Edit:
This question was tagged with magento-1.7/magento-1.8 ... just to complete the answer:
changeTableAutoIncrement
was added in 1.8.0.1truncateTable
was added in 1.6.0.0-alpha1If if want to truncate the custom table in your controller, Cron, and Block so you can first use create the modal/collection on their constructor and use the below structure for the same, and also you can use code as per your requirement.: Below patterns is used in Magento 2.
use Vendor\Module\Model\ModelFactory;
.....
class Collect extends ....
{
protected $_modelFactory;
......
public function __construct(
.....
.....
ModelFactory $modelFactory
) {
.....
$this->_modelFcatory = $modelFactory;
}
public function execute()
{
$connection = $this->_modelFactory->create()->getCollection()->getConnection();
$tableName = $this->_modelFactory->create()->getCollection()->getMainTable();
$connection->truncateTable($tableName);
}
}
Hope This helps You.
I have create a Model and for some development i had to implement delete method. It is very easy. I also try for conditional delete but i didn't get exact code. But i am using the following code which will fulfill your requirement for conditional delete.
$items = Mage::getModel('custom/csaabandonedcart')->getCollection()
->addFieldToFilter('csa_id',Mage::getSingleton('customer/session')
->getCustomerId())->addFieldToFilter('customer_id', $retailer)
->addFieldToFilter('product_id', $spid);
foreach ($items as $item) {
$item->delete();
}