How can I convert the result of Trips::model()->findAll()
to an array?
Use DAO for arrays
$array = Yii::app()->db->createCommand('SELECT * FROM tbl')->queryAll();
You can create collections CMap continue to work with her
$collections = new CMap();
foreach (YourModel::model()->findAll(['index' => 'id']) as $key => $row) {
$collections->add($key,$row->attributes);
}
var_dump($collections ->toArray());
You can use this.
$Trips::model()->findAll(array('index'=>'trip_id'));
if(count($Trips)>0)
{
$TripsArrayList=array();
foreach($Tripsas as $singleTrip)
{
$TripsArrayList[]=array('trip_id'=>$singleTrip->trip_id,'name'=>$singleTrip->name);
}
}
Your output will be
Array
(
[0] => Array
(
[trip_id] => 1
[name] => Nashik
)
[1] => Array
(
[trip_id] => 2
[name] => Puna
)
[2] => Array
(
[trip_id] => 3
[name] => Mumbai
)
)
Assuming from your question that you want all the attributes, a more compact solution to give you all attributes hashed by id, you can use CActiveRecord's 'attributes' pseudoproperty as follows:
CHtml::listData(Trips::model()->findAll(), 'id', 'attributes')
$cats = Category::model()->findAll();
$count_cats = count($cats);
if($count_cats > 0){
$arr_category = array();
foreach($cats as $cat)
array_push($arr_category,$cat->attributes);
}
print_r($arr_category);
-> result
Array(
[0] => Array
(
[cat_id] => 2
[title] => Đương đại
[title_full] => Đương đại
[desc] =>
[alias] => duong-dai
[p_id] => 0
[type] => 1
[status] => 1
[sort_order] => 2
[selected] => 0
)
[1] => Array
(
[cat_id] => 164
[title] => Nhiệt đới
[title_full] => Nhiệt đới
[desc] =>
[alias] => nhiet-doi
[p_id] => 0
[type] => 1
[status] => 1
[sort_order] => 0
[selected] => 0
)
[...])
Don't used CHtml::listData for this. It has to be used for other purposes. There is an index property of CDbCriteria which is suitable for you requirement.
//1st option
Trips::model()->findAll(array('index'=>'trip_id'));
//2nd option
$c = new CDbCriteria();
$c->index = 'trip_id';
Trips::model()->findAll($c);