Truncate Table from Database using Data Patch Interface in Magento 2 (Method 2)
Step 1: Create a truncatetable.php file in your Magento root directory. Here we need to use the model factory class to truncate the table.
Add the code as follows
<?php
use Magento\Framework\App\Bootstrap;
require __DIR__ . '/app/bootstrap.php';
$bootstraps = Bootstrap::create(BP, $_SERVER);
$object_Manager = $bootstraps->getObjectManager();
$app_state = $object_Manager->get('\Magento\Framework\App\State');
$app_state->setAreaCode('frontend');
try
{
$bootstrap = \Magento\Framework\App\Bootstrap::create(BP, $_SERVER);
$objectManager = $bootstrap->getObjectManager();
$appState = $objectManager->get('\Magento\Framework\App\State');
$appState->setAreaCode('frontend');
$objectManager= \Magento\Framework\App\ObjectManager::getInstance();
// enter your Extension Factory Class
$locationTruncate = $objectManager->get('\Vendor\Extension\Model\LocationFactory')->create();
$connection = $locationTruncate->getCollection()->getConnection();
$tableName = $locationTruncate->getCollection()->getMainTable();
$connection->truncateTable($tableName);
}
catch(\Exception $e)
{
print_r($e->getMessage());
}
Comments
Post a Comment