很多时候 Magento 2 表名都有每个表的前缀值。
我们可以在 Magento 2 中通过以下方式获取表名前缀。
使用下面的代码片段,您可以始终获得带有前缀代码的表格,
<?php
public function __construct(
\Magento\Framework\App\ResourceConnection $resource
) {
$this->resource = $resource;
}
public function customHandler()
{
/* Create Connection */
$connection = $this->resource->getConnection();
$tableName = $connection->getTableName('sales_order'); // return "prefix_sales_order"
/* Run row query */
$query = "SQL Query";
$connection->query($query);
}
In above function your table will get prefix value with table name.
If your table has prefix as mage_ then your table value will get like, mage_sales_order