1

How will you join flat table and EAV table in Magento?

2
$productCollection->joinTable(
    array('bonus' => 'mycompany/bonus'), 'product_id=entity_id',
    array('bonus_id' => 'bonus_id')
);

The parameters are:

public function joinTable($table, $bind, $fields = null, $cond = null, $joinType = 'inner')

Table is easy, it is the magento namespace/entity format, which you use in your configuration, resource models and the collection.

Bind means, the ON statement in your SQL

You can use an array of the format array('alias' => 'namespace/entity')

It is important to have your flat table BEFORE and your EAV table AFTER the equal sign. Don't use main_table. before the attribute. Magento will do this for you.

Fields is an array. If you use a string instead, you get this:

Warning: Invalid argument supplied for foreach() in /var/www/magento-1.6.2.0/app/code/core/Mage/Eav/Model/Entity/Collection/Abstract.php on line 775.

You can use an array of the format array('field1', 'field2', '...') or array('alias' => 'field1', '...')

Condition is a * WHERE ON condition* in the SQL.

Source: fabian-blechschmidt

| 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.