6

I've got a productcollection, which i joined a custom table to. I'd now like to order the collection on the custom joined table row "custom_row_value".

I've tried the below method, but this does not work.

$_category = Mage::registry('current_category');

$_productCollection = Mage::getModel('catalog/product')
->getCollection()
->addAttributeToFilter('entity_id', array('in' => $_collectProductIds));

$_productCollection
->getSelect()
->join('custom_table', 'custom_table.entity_id = e.entity_id', array('custom_row_value'))
->where('custom_table.category_id='.$_category->getId())
->order('custom_table.custom_row_value','desc');

How would i achieve this goal in a neat way?

1
  • What kind of effect are you having? No sorting or errors? – Phoenix128_RiccardoT Feb 26 '16 at 21:40
5

Your code is correct, what is probably wrong is the order direction.

According to Zend_Db_Select::order definition you can pass only one argument, not two arguments as you are doing in your code.

Your query will be handled regardless the sorting direction and will take ASC as default.

Try changing:

->order('custom_table.custom_row_value','desc');

To

->order('custom_table.custom_row_value desc');

You can check your query with a simple:

echo $_productCollection->getSelect();

If it does not work, then you problem is elsewhere IMHO.

Hope it helps.

1

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.