Kohana 3 ORM as_array 返回 ORM 数组
我正在执行一个简单的查询并想要返回一个数组。 根据 Kohana 3 指南附带的 ORM 教程,我认为我可以执行以下操作:
ORM::factory('user')->find_all()->as_array();
但这似乎给了我一个模型对象数组(即 array( User_Model1, User_Model2 ...
看看源代码,我发现我可以通过使用以下补丁可以轻松解决此问题,
modules/database/classes/kohana/database/result.php
@@ -94,7 +94,7 @@
foreach ($this as $row)
{
- $results[] = $row;
+ $results[] = $row->as_array();
这似乎更符合用户指南的内容:
ORM 的一个强大功能是 ORM::as_array 方法,它将以数组形式返回给定的记录。如果与 ORM::find_all 一起使用,将返回所有记录的数组。对于选择列表来说这是有用的一个很好的例子:
// 显示用户名的选择字段(使用 id 作为值) echo Form::select('user', ORM::factory('user')->find_all()->as_array('id', 'username'));
想知道这是否是故意的,如果是的话,为什么? 如果我确实想创建关联数组的数组,那么更好的解决方法是什么?
I'm executing a simple query and want to get an array back.
Based on the ORM tutorial that comes with the Kohana 3 guide, I thought I could do the following:
ORM::factory('user')->find_all()->as_array();
But that seems to give me an array of model objects (ie array( User_Model1, User_Model2 ...
Looking at the source I see I can easily fix this by hacking with the following patch.
modules/database/classes/kohana/database/result.php
@@ -94,7 +94,7 @@
foreach ($this as $row)
{
- $results[] = $row;
+ $results[] = $row->as_array();
Which seems to be more in line with what the user guide says:
A powerful feature of ORM is the ORM::as_array method which will return the given record as an array. If used with ORM::find_all, an array of all records will be returned. A good example of when this is useful is for a select list:
// Display a select field of usernames (using the id as values) echo
Form::select('user', ORM::factory('user')->find_all()->as_array('id', 'username'));
Wondering if this is intentional, if so, why?
What would be a better work around if I do want to create an array of associative arrays?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这是有意为之的行为,(显然?)在文档中可见,因此请不要应用该“补丁”。特别是因为您想要(不仅仅是)修改 ORM 本身。
相反,请阅读以下内容:
as_array()
应用于行集合,则它返回行数组(每行都是单独的对象,而不是数组),因此您至少有两种解决方案:
这个特定的类称为
Kohana_Database_Result
,因此将类Database_Result
放置在application/class/database/result.php
中,并使其继承自Kohana_Database_Result
,然后更改您需要的内容(如果您需要更改)。This is intentional behaviour, as (clearly?) visible in the documentation, so please do not apply that "patch". Especially because you want to modify ORM (not only) itself.
Instead read this:
as_array()
is applied on the collection of rows, it returns array of rows (each row being separate object, not array),So you have at least two solutions:
This specific class is called
Kohana_Database_Result
, so place classDatabase_Result
inapplication/class/database/result.php
and make it inherit fromKohana_Database_Result
, then change what you need (if you need to change that).您正在使用 ORM,但似乎您并不真正需要它的功能(其中包括将数据库行作为模型对象返回)。在这种情况下,解决方案可能是直接使用 Database 类。在您的模型中,您可以执行以下操作:
默认情况下
Database::query()
将返回一个关联数组。You are using the ORM but it seems you don't really need its features (which is, among others, to return database rows as Model objects). In that case, a solution could be to use the Database class directly. From your model, you can do:
By default
Database::query()
will return an associative array.ORM 没有像你描述的那样的方法。有一个 select_list( ) Kohana 2.3.4 中的方法,可能是用户指南包含旧(或未来)版本中的错误信息?
附言。并且没有理由更改 DB Result 对象,因为
$row
可以是任何(不仅仅是 ORM)类。ORM has no methods like you describe. There was a select_list() method in Kohana 2.3.4, may be userguide contains wrong information from old (or future) version?
PS. And there is no reason for changing DB Result object, because
$row
may be any (not only ORM) class.