Hoa_Ban
(Hoa Ban)
January 16, 2016, 3:32am
1
Hello Guys
I need to get prefix table in magento 2 to join with another table. I try to find on internet but I don’t see how to get prefix table in magento 2. Can you help me?
You should call the \Magento\Framework\App\ResourceConnection::getTableName()
method:
public function getTableName($modelEntity, $connectionName = self::DEFAULT_CONNECTION)
{
$tableSuffix = null;
if (is_array($modelEntity)) {
list($modelEntity, $tableSuffix) = $modelEntity;
}
$tableName = $modelEntity;
$mappedTableName = $this->getMappedTableName($tableName);
if ($mappedTableName) {
$tableName = $mappedTableName;
} else {
$tablePrefix = $this->getTablePrefix();
if ($tablePrefix && strpos($tableName, $tablePrefix) !== 0) {
$tableName = $tablePrefix . $tableName;
}
}
if ($tableSuffix) {
$tableName .= '_' . $tableSuffix;
}
return $this->getConnection($connectionName)->getTableName($tableName);
}
A usage example