How can I convert the result of Trips::model()->findAll()
to an array?
相关问题
- How to get the maximum of more than 2 numbers in V
- Faster loop: foreach vs some (performance of jsper
- Convert Array to custom object list c#
- pick a random item from a javascript array
- Newtonsoft DeserializeXNode expands internal array
相关文章
- laravel create model from custom stub when using p
- Numpy matrix of coordinates
- Can you run console jobs from yii2-basic?
- PHP: Can an array have an array as a key in a key-
- How to do a bulk database insert in Yii2?
- Accessing an array element when returning from a f
- How can I convert a PHP function's parameter l
- How to make a custom list deserializer in Gson?
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:
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.-> result
Use simply:
Note: This is not good way but returns array
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.
i use
$array = CJSON::decode(CJSON::encode($model));
to convert $model to $array.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:
output: