SELECT DISTINCT( monthname( date_purchased ) ) AS month, month( date_purchased ) AS m FROM orders WHERE date_purchased LIKE ‘2010-%’ ORDER BY date_purchased DESC
I want above query with FindAll function how this possible ?
$results=ModelName::model()->findAllBySql('SELECT DISTINCT( monthname( date_purchased ) ) AS month, month( date_purchased ) AS m FROM orders WHERE YEAR(date_purchased ) = 2010 ORDER BY date_purchased DESC');
and define $month and $m as attributes in your model so that these can be accessed when in loop:
foreach($results AS $result){
echo $result->month;
echo $result->m;
}
If i were you i would just use DAO:
$sql='SELECT DISTINCT( monthname( date_purchased ) ) AS month, month( date_purchased ) AS m FROM orders WHERE YEAR(date_purchased ) = 2010 ORDER BY date_purchased DESC';
$connection=Yii::app()->db;
$command=$connection->createCommand($sql)
$results=$command->queryAll();
foreach($results AS $result){
echo $result['month'];
echo $result['m'];
}