3

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

5 Answers 5

7

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',
    // ...
}
Sign up to request clarification or add additional context in comments.

1 Comment

Kludge! See the answer of @Telvin Nguyen. But better is use an anonymous function to extract result from findAll as a array.
1

An active record, by it's name, will return a whole record for each row in your database, so the best way to obtain just one field from each row would be using the querybuilder with something like what you've got above.

If you really wanted to use AR and just want the name in an array then something like this might work:

$names = array();
$users = users::model()->findAllByAttributes(array('city'=>'Paris'));
foreach(array_keys($users) as $key)
{
    $names[] = $users[$key]->name;
}

Although that's a lot of overhead to pull just the name if you're not using any other details from the AR search.

Comments

1

Actually I have dealt with the same problem, I would like to use the ActiveRecord instead of CDBCommand. I have answered on the similar question before

get array from active record

$words = Word::model()->findAll();
$data=array_map(create_function('$m','return $m->getAttributes();'),$words);
var_dump($data);

2 Comments

doesn't ActiveRecord return objects? array_map expects parameter 2 to be an array - but since $words is an object, we get an error. (?)
@Steven CActiveRecord comprises the findall method return the array of records found. An empty array is returned if none is found. In my example, the $words variable always be an array.
0

As already mentioned, you can use findAllByAttributes(array $attributes, mixed $condition='', array $params=array ( )) and also tell this method to fetch only the 'name' column for the records it matches. To do this, you'll need to pass as $condition an instance of CDbCriteria and specify on this instance the 'select' columns.

Comments

0

Use Chtml to this is a Ugly Hack! In short, you can do this:

public function queryAll($condition = '', $params = array())
{
    $criteria = $this->getCommandBuilder()->createCriteria($condition, $params);
    $this->applyScopes($criteria);
    $command = $this->getCommandBuilder()->createFindCommand($this->getTableSchema(), $criteria);
    $results = $command->queryAll();
    return $results;
}

Follow my answer to a identical problem in other question, in this link.

Comments

Your Answer

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