Magento 2 Custom Controller File – Get Table Name with Prefix

databasemagento2prefix

public function checkHelpful($rid ,$pid ,$uid )
{
    $connection = $this->objectManager->get('Magento\Framework\App\ResourceConnection');
    $tbl = $connection->getTableName('catalog/product_index_price');
    echo $table;
}

the output of this is 'catalog/product_index_price'. Not getting the exact table name with prefix.

Best Answer

Try this

 protected $_resource;

 public function __construct(
    \Magento\Backend\App\Action\Context $context,
    \Magento\Framework\App\ResourceConnection $resource
    ) {
    $this->_resource = $resource;
    parent::__construct($context);
}

public function execute()
{

    $connection  = $this->_resource->getConnection();
    $tableName   = $connection->getTableName('tablename_without_prefix'); // It will return table with prefix

    $mapsDeleteQuery = "your raw SQL Query";
    $connection->query($mapsDeleteQuery);
}
Related Topic