如何在 Magento 2 中获取带前缀的表名?

很多时候 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

版权属于: sbboke版权所有。

转载时必须以链接形式注明作者和原始出处及本声明。

张贴在magento2教程标签:

相关文章

0 0 投票数
文章评分
订阅评论
提醒
0 评论
内联反馈
查看所有评论