Yii model to array?

前端 未结 14 1740
南笙
南笙 2020-12-08 19:32

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

相关标签:
14条回答
  • 2020-12-08 20:16

    Use DAO for arrays

    $array = Yii::app()->db->createCommand('SELECT * FROM tbl')->queryAll();
    
    0 讨论(0)
  • 2020-12-08 20:18

    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());
    
    0 讨论(0)
  • 2020-12-08 20:19

    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
                     ) 
        )
    
    0 讨论(0)
  • 2020-12-08 20:19

    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')
    
    0 讨论(0)
  • 2020-12-08 20:21
    $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
        )
    [...])
    
    0 讨论(0)
  • 2020-12-08 20:21

    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);
    
    0 讨论(0)
提交回复
热议问题