1

I am trying to return a collection of brands in Magento that are grouped by the first letter of the title attribute. I have this but it results in a 500 error on the page in question:

public function getBrandGroupedCollection()
    {

        $brandsCollection = Mage::getModel('shopbybrand/brand')->getCollection();
        $brandsCollection
            ->addStatusFilter()
            ->setOrder('title', 'asc')
        ;
        $brandsCollection->getSelect()->group(substr('title',0,1)); 
        return $brandsCollection;
    }

How can I group the results by the first letter of the "title" attribute?

1 Answer 1

1

I think you'd have to perform an expression on the query into a new column that you may then aggregate on. The way you're doing it in the sample above would try to group by the results by the value of a column named t which does not exist.

Try this:

public function getBrandGroupedCollection()
{

    $brandsCollection = Mage::getModel('shopbybrand/brand')->getCollection();

    $brandsCollection
        ->addStatusFilter()
        ->setOrder('title', 'asc')
    ;

    $brandsCollection->getSelect()
        ->columns(new Zend_Db_Expr('SUBSTR(title, 1, 1) AS first_letter'))
        ->group('first_letter');

    // Optional debug output
    //echo $brandsCollection->getSelect()->assemble(); exit;

    return $brandsCollection;
}

So we rely on this dynamic column first_letter to be generated using MySQL's built-in SUBSTR [ref]. Notice that its index is 1-based (not zero). Now you can group by this column.

Note: Not sure if it will make a difference, but you could base your ordering then on first_letter instead of title

4
  • Hi and thanks! I think this brings me much closer. When I added the column and grouped by it I get another 500 error. Here are the details of the log: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'first_letter' in 'group statement'";i:1;s:3791:"#0 /public_html/lib/Varien/Db/Statement/Pdo/Mysql.php(110): Zend_Db_Statement_Pdo->_execute(Array)
    – Kray
    Mar 20, 2015 at 11:27
  • The column must not be getting added (see line 11 of my code above) to the query. Verify the query by dumping it before you return the collection. See my updated answer for how to do that. Then post the output query. Mar 20, 2015 at 17:16
  • Output from the debug is this: SELECT main_table.*, SUBSTR(title, 1, 1) AS first_letter FROM shopbybrand_brand_flat AS main_table WHERE (brand_status = 1) GROUP BY first_letter
    – Kray
    Mar 20, 2015 at 22:08
  • I did some debugging on the added line and discovered that the column is being added. If I remove "->group('first_letter');" everything works fine. That must be where the problem is...
    – Kray
    Mar 21, 2015 at 15:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.