4

I am doing some raw sql opertaions and would like to know if there is a way to get programmatically the Prefix used for the tables (the prefix you set during the Magento installation)?

3
  • The prefix is set in the app/etc/env.php file. Just parse that array out and grab the value for 'table_prefix'
    – tecjam
    Commented Apr 14, 2016 at 12:14
  • what's the correct way of getting the values from env?
    – Rahahaha
    Commented Apr 14, 2016 at 12:53
  • You can grab them via the Magento\Framework\App\DeploymentConfig and the getConfigData method.
    – tecjam
    Commented Apr 14, 2016 at 13:37

3 Answers 3

9

You can get the prefix by using the function getTableName() like below : Always use getTableName with raw queries to fetch the table along with the prefix dynamically

 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   = $this->_resource->getTableName('tablename_without_prefix'); // It will return table with prefix

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

You can also get the prefix from the file app/etc/env.php

'db' => 
  array (
    'table_prefix' => '', // if any prefix is used, you can see here
    'connection' => 
    array (
      'default' => 
      array (
        'host' => 'localhost',
        'dbname' => 'magento2',
        'username' => 'abc',
        'password' => 'abc',
        'active' => '1',
      ),
    ),
  ),
6
  • 2
    i tested this but it's not return the table name with the prefix. its just return the table name. on your example, its still returning customer_entity instead of the expected prefix_customer_entity.
    – Rahahaha
    Commented Apr 14, 2016 at 12:50
  • check my edited answer Commented Apr 14, 2016 at 12:58
  • hi manashvi, how do you get that array in the env.php? does magento have a method to get it?
    – Rahahaha
    Commented Apr 14, 2016 at 13:03
  • No there is no such way to fetch the array in env.php. The code specified above that should work. Commented Apr 14, 2016 at 13:07
  • 3
    oh ok i now get the table name with prefix. the correct code is $this->_resource->getTableName("tablename without prefix");
    – Rahahaha
    Commented Apr 14, 2016 at 13:11
4

I know this is an old topic, but the right answer is not the accepted one, so I felt I should share the right code for someone who's looking for this.

The code below works:

 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()
 {
    $this->_resource->getTableName("tablename without prefix");
 }

The code below does not work, and will not do anything. Note that there is a crucial difference:

$connection  = $this->_resource->getConnection();
$tableName   = $connection->getTableName('tablename_without_prefix');
2
  • Works, thank you!
    – snez
    Commented Sep 20, 2021 at 9:58
  • works for me, thanks
    – Jiyaad.D
    Commented Jul 12, 2022 at 13:34
3
    $bootstrap = \Magento\Framework\App\Bootstrap::create(BP, $_SERVER);    
    $obj = $bootstrap->getObjectManager();
    $deploymentConfig = $obj->get('Magento\Framework\App\DeploymentConfig');
    var_dump($deploymentConfig->get('db/table_prefix'));

References:

Magento\Framework\App\DeploymentConfig

Magento's Deployment Configuration

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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