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.


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.

| improve this answer | |

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);
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.