Consider this query and its result:
Yii::app()->db->createCommand(
"SELECT name FROM users WHERE city='Paris'"
)->queryColumn();
Result:
Array
(
[0] => John Kerry
[1] => Marry White
[2] => Mike Jain
)
Any suggestions on how to build this query with ActiveRecord?
It is necessary to receive the array.
Duplicate: Yii - How to get a values array from an Active Record
use CHtml::listData (see http://www.yiiframework.com/wiki/48/by-example-chtml/#hh3 )
$users = User::model()->findAll();
$usersArr = CHtml::listData( $users, 'id' , 'city');
print_r( $usersArr );
It will give you array id => city
Array {
2 => 'Paris',
102 => 'Riga',
// ...
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With