Magento 2 – Get Table Name on Collection Join

collection;databasemagento2resources

I have a Magento 2 collection and I want to JOIN more tables to this collection's select:

public function prepareProductCollection(\Magento\Catalog\Model\ResourceModel\Product\Collection $collection) {
    $query = $collection->getSelect();
    $alias = 'mot';
    $name = 'my_own_table';
    $on = "...";
    $cols = ['col1', 'col2'];
    $query->joinLeft([$alias => $name], $on, $cols);        
}

How can I get table name with prefix if I have $collection variable only? This case returns table name itself (w/o prefix):

$query->getConnection()->getTableName('my_own_table')

Best Answer

You can use

$collection->getResource()->getTable('YOUR_TABLE');

Even this class is also getting table name with this way. Just look at \Magento\Catalog\Model\ResourceModel\Product\Collection this

$this->getResource()->getTable('catalog_product_website');

Related Topic