Magento - Unable to set order of collection

  • This doesn't seem to be ordered correctly, anything I'm doing wrong? Suggestions?

    $componentQuantityCollection = Mage::getModel('catalog/product')->getCollection();
    $componentQuantityCollection->addAttributeToFilter('sku', array('in' => $componentSkus))->setOrder('sku','ASC');

    Another collection that doesn't seem to be sorted which is different than the first:

    $kitCollection = Mage::getModel('kitinventory/kitinventory')->getCollection()->addFieldToFilter('kit_sku', $sku)->setOrder('related_sku', 'DESC');
  • EAV collections work with attributes, the sorting method is a little different here as well

    $componentQuantityCollection->addAttributeToSort('sku', 'ASC');

    For non-EAV collections use one of the following methods

    $kitCollection->getSelect()->order('related_sku DESC');
    $kitCollection->setOrder('related_sku', 'DESC');

    what about the second collection?

    That's a flat type collection, so no EAV and attributes. Take a look at this answer on how to sort that:

    I tried setOrder('related_sku','DESC'); but it's not sorted.

    I've edited my answer

    Ah, gracious -- still unsure why you can't just chain them in a similar manner.

    The non-EAV approach worked for me. Although, in my case it wasn't working because addAttributeToSort() was adding an IF statement with back ticks all over the place. In result, MySQL kept throwing an error. Thanks.

    @easymoden00b use this `$kitCollection->getSelect()->order('related_sku DESC');`

License under CC-BY-SA with attribution

Content dated before 7/24/2021 11:53 AM