Yii model to array?

2019-01-17 06:27发布

How can I convert the result of Trips::model()->findAll() to an array?

标签: arrays model yii
14条回答
够拽才男人
2楼-- · 2019-01-17 06:47

I'm going on the assumption here that you only need to retrieve just the bare arrays, and not any associated model objects.

This will do it:

$model = Trips::model();
$trips = $model->getCommandBuilder()
               ->createFindCommand($model->tableSchema, $model->dbCriteria)
               ->queryAll();

This is like the Yii::app()->db->createCommand('SELECT * FROM tbl')->queryAll(); examples, except:

  • It'll ask the model for the table name; you won't need to write the table name in both the model and the query.

  • You can call scoping functions on $model first, eg.
    $model = Trips::model()->short()->destination('Austin, TX');
    Doing this means you can use the model's existing query shortcuts, instead of putting them in the query directly.

In contrast, the $trips = Trips::model()->findAll(); (using foreach) is a bit wasteful, in that you're pulling the rows from the database, setting up a bunch of objects, and then throwing them all away. It'll work fine for small result sets, but I wouldn't use that if you're looking at a long list of Trips.

Caveat:
If this is just a quick prototype, though, by all means use the createCommand() or findAll()-and-loop examples.

查看更多
别忘想泡老子
3楼-- · 2019-01-17 06:48
$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
    )
[...])
查看更多
Animai°情兽
4楼-- · 2019-01-17 06:50

Use simply:

$trips = Trips::model()->findAll();

$trips_array = CJSON::decode(CJSON::encode($trips));

Note: This is not good way but returns array

查看更多
唯我独甜
5楼-- · 2019-01-17 06:57

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);
查看更多
Emotional °昔
6楼-- · 2019-01-17 06:58

i use $array = CJSON::decode(CJSON::encode($model)); to convert $model to $array.

查看更多
女痞
7楼-- · 2019-01-17 07:01

Easy and simple way: I use listData() method to make array to dropdown menus, and I think this will help you.. check this example:

code:

<?php 
     /*you can use here any find method you think 
     proper to return your data from db*/
     $models = Trips::model()->findAll();

     // format models resulting using listData     
     $tripsArray = CHtml::listData($models, 'id', 'name');    

     print_r($tripsArray);
?>

output:

array(
 '1'=>'trip1',
 '2'=>'trip2',
 '3'=>'trip3',
)
查看更多
登录 后发表回答