Yii的模式阵列? [英] Yii model to array?
问题描述
我怎么能行程的结果::模型()转换 - >的findAll()
到一个数组
推荐答案
我要去假设在这里,你只需要检索只是光秃秃的阵列,并没有任何关联的模型对象。
I'm going on the assumption here that you only need to retrieve just the bare arrays, and not any associated model objects.
这将做到这一点:
$model = Trips::model();
$trips = $model->getCommandBuilder()
->createFindCommand($model->tableSchema, $model->dbCriteria)
->queryAll();
这是类似于的Yii ::应用() - GT; DB-> createCommand(SELECT * FROM TBL) - > 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.
您可以拨打<$ 作用域功能C $ C> $模型第一,如:结果 $型=游::模型() - GT;短() - GT;目的地(美国德克萨斯州奥斯汀');
结果
这样做意味着你可以使用模型的现有查询的快捷方式,而不是把他们直接查询。
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.
在此相反, $ =车次旅行::模型() - GT;的findAll();
(使用的foreach)是有点浪费,因为你从数据库中提取的行,设置了一堆的对象,然后把他们全部带走。它会工作的优良小的结果集,但我不会使用,如果你正在寻找旅行的一个长长的清单。
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.
警告:结果
如果这仅仅是一个快速原型,不过,通过各种手段使用 createCommand()
或的findAll() - 和环例子
Caveat:
If this is just a quick prototype, though, by all means use the createCommand()
or findAll()-and-loop examples.
这篇关于Yii的模式阵列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!