0

Is there a way to sort a collection of cms blocks? I need to sort blocks by identifier. I tried to do it similar to a collection of products:

$collection = Mage::getModel('catalog/product')
    ->getCollection()
    ->addAttributeToSort('name', 'DESC')
    ->load();

but it will not work because there is no addAttributeToSort method in the block collection object:

$collection = Mage::getModel('cms/block')
    ->getCollection()
    ->addAttributeToSort('identifier', 'DESC')
    ->load();

Is there any workaround for this?

2

addAttributeToSort use for Eav type Model Catalog,Product,customer. For Flat structure Model collection is not working except Sales model. Also working on Sales Models

CMS module's Models are use Flat structure,So you don't use the addAttributeToSort .

You Should use setOrder('[FieldName]','[ORDER]') function.

$collection = Mage::getModel('cms/block')
    ->getCollection()
    ->load();
$collection->setOrder('identifier', 'DESC');
  • Should I use ->setOrder on the collection after I call ->load() or before? – zitix May 1 '17 at 15:48
0

After all those months I still meeting some problems ;) Again, trying to sort something but Magento is like a stubborn donkey, whatever I do it won't do what I want

        $ar_blockIds = Mage::getModel('cms/block')
        ->getCollection()
        ->addFieldToFilter('is_slide',true)
        ->addFieldToFilter('is_active',true)
        ->addFieldToFilter('start_time', array('lteq' => $now))
        ->addFieldToFilter('end_time', array('gteq' => $now))
        ->getAllIds()
    ;

How to sort ids by position ? There is a field in table where position is stored but collection won't sort it .... What am I doing wrong ?

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.