Kohana 3 ORM as_array 返回 ORM 数组

发布于 2024-09-29 22:59:57 字数 892 浏览 12 评论 0原文

我正在执行一个简单的查询并想要返回一个数组。 根据 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

寂寞美少年 2024-10-06 22:59:58

这是有意为之的行为,(显然?)在文档中可见,因此请不要应用该“补丁”。特别是因为您想要(不仅仅是)修改 ORM 本身。

相反,请阅读以下内容:

  • 如果将 as_array() 应用于行集合,则它返回行数组(每行都是单独的对象,而不是数组),
  • 如果应用于 < strong>单行,它将行作为数组返回,

因此您至少有两种解决方案:

  • 将集合转换为数组后显式转换每一行,
  • 在代理类中添加您自己的方法而不是更改模块代码( Kohana 有从核心类继承的空类,您可以通过将它们放置在应用程序/类中来覆盖它们。

这个特定的类称为 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:

  • if as_array() is applied on the collection of rows, it returns array of rows (each row being separate object, not array),
  • if applied on single row, it returns the row as an array,

So you have at least two solutions:

  • transform each row explicitly after transforming collection to array,
  • add your own methon in the proxy class instead of changing the module code (Kohana has empty classes that inherit from core classes and that you can override by placing them in application/classes).

This specific class is called Kohana_Database_Result, so place class Database_Result in application/class/database/result.php and make it inherit from Kohana_Database_Result, then change what you need (if you need to change that).

黒涩兲箜 2024-10-06 22:59:58

您正在使用 ORM,但似乎您并不真正需要它的功能(其中包括将数据库行作为模型对象返回)。在这种情况下,解决方案可能是直接使用 Database 类。在您的模型中,您可以执行以下操作:

$output = $this->_db->query(Database::SELECT, "select * from users");

默认情况下 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:

$output = $this->_db->query(Database::SELECT, "select * from users");

By default Database::query() will return an associative array.

阳光下慵懒的猫 2024-10-06 22:59:58

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文