English 中文(简体)
Kohana 3 ORM, as_array 回流阵
原标题:Kohana 3 ORM as_array return array of ORM

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();

但是,这似乎给我提供了一系列示范目标(用户_Model1,用户_Model2......)。

看看来文方一看,我很容易用下面的拼凑来解决这一问题。

modules/database/classes/kohana/database/result.php
@@ -94,7 +94,7 @@
                        foreach ($this as $row)
                        {
-                               $results[] = $row;
+                               $results[] = $row->as_array();

似乎更符合用户指南的内容:

人力资源管理厅的一个强有力的特征是:作为信息处理方法,将特定记录作为阵列。 如果与人力资源管理厅使用:经确定后,将归还所有记录。 选择清单就是一个很好的例子:

// 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?

问题回答

这是一种故意的行为,因为文件(明确吗?)如此,please并不适用于“到。 尤其是因为你想要改变办公室管理(不仅仅是)本身。

改为:

  • 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,

因此,你们至少有两个解决办法:

  • 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).

这一具体类别称为Kohana_Database_Result,这样就改变了您的需要(如果需要改动的话)。

你正在使用管理办公室,但你似乎并不真正需要其特征(除其他外,这是为了把数据库浏览作为示范目标)。 在这种情况下,解决办法可以是直接使用数据库类别。 根据您的模式,您可以:

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

通过违约<代码>数据库:频率()将退回一个联系阵列。





相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...