Using the following code:
$collection = Mage::getModel('custom/custom')
->getCollection()
->distinct(true)
->addFieldToSelect('brand')
->load();
I've found the distinct is being ignored in 1.4.2, exactly the same code in 1.8.1 is working as I would expect (returning a single entry for each brand).
1.4.2 result:
"Ace"
"Ace"
1.8.1 result:
"Ace"
For myself I notice that 1.4.2 is returning an array of all fields from the model for each object , rather than just the one specified in the select (brand) as 1.8.1 does. I think this is what's causing the problem the distinct is included in the SELECT on both versions - in fact the SELECT statements are identical across versions.
Is there a way to get a distinct result for a single field in older versions of Magento?
EDIT
Based on the discussion below with liyakat my final solution was:
$collection = Mage::getModel('custom/custom')
->getCollection()
->distinct(true)
->addFieldToSelect('brand')
->setOrder('brand', 'ASC');
$collection->getSelect()->group('brand');
$collection->load();
you can add this function to get select statement of collection. and i am sure you will get result.
$collection = Mage::getModel('custom/custom')
->getCollection()
->addFieldToSelect('brand')
->load();
$collection->getSelect()->columns(
array('asbrand' => new Zend_Db_Expr(
"distinct(brand)")
)
);
hope this will work for you.