2

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')
2

1 Answer 1

5

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');

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.