Magento: $collection->join() Does Not Allow Table Aliases

I am working on a custom module, and I ran into the situation where I need to join the same table to my collection twice. Here is what I tried to do, which does not work:

    ->join('color', 'color.color_id = main_table.rug_color_id', ' as rug_color_name')
    ->join('color', 'color.color_id = main_table.binding_color_id', ' as binding_color_name')
    ->join('binding', 'binding.binding_id = main_table.binding_id', ' as binding_name');

The above results in the top and the bottom join working fine, but the middle one is skipped. The reason is that the join() method inherited by the collection does not allow you to pass in a table alias. So, I discovered a different way of adding a join which does allow the passing of table aliases. If I remove the middle join from above, I can convert it to this which makes it work as needed:

    array('binding_color' => $collection->getTable('color')),
    'binding_color.color_id = main_table.binding_color_id',
    ' as binding_color_name'
This entry was posted in Magento. Bookmark the permalink.

One Response to Magento: $collection->join() Does Not Allow Table Aliases

  1. Su says:

    Thank you very much. You saved my day :)