Yii model to array?

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

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

14条回答
  •  隐瞒了意图╮
    2020-12-08 20:03

    This is the right way to do, it follows Yii conventions

    $trips = Trips::model()->findAll();
    $arr = array();
    foreach($trips as $t)
    {
        $arr[$t->id] = $t->attributes;
    }
    

    This is used when you have complex queries, those you find difficult to create with Yii conventions.

    Yii::app()->db->createCommand('SELECT * FROM tbl')->queryAll();
    

    For example, when you need to pass all the data from the model to an array. You cannot pass it directly as it does pass some ActiveRecord data information that you don't need.

提交回复
热议问题