Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I recieve array using Yii ActiveRecord

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.


1 Answers

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',
    // ...
}
like image 156
briiC Avatar answered Nov 27 '25 06:11

briiC



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!