Retrieve table name from collection
-
12-12-2019 - |
Question
How can I retrieve table name from Magento collection. My case: observer listens catalog_product_collection_load_before
event. Here we can get product collection. But I need exactly which table is used for this collection. Magento uses flat tables if it is enabled, otherwise catalog_product_entity
table. I have enabled flat tables but sometimes Magento uses eav table for product collection (on catalog/category/view request). Any suggestions will be appreciated.
Solution
Here is a way to do it.
Let's say $collection
is your product collection.
$from = $collection->getSelect()->getPart('from');
$table = $from['e']['tableName'];
$table
is what you are looking for.
OTHER TIPS
A collection can have multiple tables associated to it depending on whether joins are used or not, but if you want to pull the main table (the one used when calling from()
on the select object) then you can call:
$table = $collection->getMainTable();
Outside of this if you want to find the any other tables involved you will need to pull from
from the select object and inspect the results:
$from = $collection->getSelect()->getPart(Zend_Db_Select::FROM);