Yii 模型要数组吗?
如何将 Trips::model()->findAll()
的结果转换为数组?
How can I convert the result of Trips::model()->findAll()
to an array?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
如何将 Trips::model()->findAll()
的结果转换为数组?
How can I convert the result of Trips::model()->findAll()
to an array?
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(15)
我在这里假设您只需要检索裸数组,而不是任何关联的模型对象。
这样就可以了:
这就像
Yii::app()->db->createCommand('SELECT * FROM tbl')->queryAll();
示例,除了:您可以在
$model 上调用 作用域 函数
首先,例如$model = Trips::model()->short()->destination('德克萨斯州奥斯汀');
这样做意味着您可以使用模型现有的查询快捷方式,而不是直接将它们放入查询中。
相比之下, $trips = Trips::model()->findAll(); (使用 foreach)有点浪费,因为您从数据库中提取行,设置一堆东西,然后把它们全部扔掉。它对于小型结果集来说效果很好,但如果您正在查看一长串旅行列表,我不会使用它。
警告:
不过,如果这只是一个快速原型,请务必使用 createCommand() 或 findAll() 和循环示例。
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.这是正确的方法,它遵循 Yii 约定。
当您有复杂的查询,并且您发现使用 Yii 约定很难创建这些查询时,可以使用此方法。
例如,当您需要将模型中的所有数据传递到数组时。您不能直接传递它,因为它确实传递了一些您不需要的 ActiveRecord 数据信息。
This is the right way to do, it follows Yii conventions
This is used when you have complex queries, those you find difficult to create with Yii conventions.
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.
这是一样的。
This is same.
简单简单的方法:我使用 listData() 方法来制作数组到下拉菜单,我认为这会对您有所帮助..检查这个示例:
代码:
输出:
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:
CHtml::listData() 将返回一个数组值。
CHtml::listData() will return an array value.
您可以创建收藏CMap继续与她合作
You can create collections CMap continue to work with her
我很确定您可以这样做:
我假设您将属性“id”作为模型的主键。
I'm pretty sure you can do this:
I'm assuming you have the attribute 'id' as your model's primary key.
我使用 $array = CJSON::decode(CJSON::encode($model)); 将 $model 转换为 $array。
i use
$array = CJSON::decode(CJSON::encode($model));
to convert $model to $array.你可以用这个。
你的输出将是
You can use this.
Your output will be
->结果
-> result
假设您的问题需要所有属性,一个更紧凑的解决方案可以为您提供按 id 散列的所有属性,您可以使用 CActiveRecord 的“属性”伪属性,如下所示:
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:
在 yii2 中你可以使用 asArray()
In yii2 you can use asArray()
对数组使用 DAO
Use DAO for arrays
不要为此使用 CHtml::listData。它必须用于其他目的。
CDbCriteria 有一个index 属性适合您的要求。
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.
简单使用:
注意:这不是好方法,但会返回数组
Use simply:
Note: This is not good way but returns array