Kohanaphp v3执行()

时间:2010-07-29 19:12:11

标签: database iteration kohana-3

excecute()方法总是在使用current()方法后返回一个“informational”对象,我总是得到一个数组。

我如何获得一个对象(带有对象)来迭代这样,(没有ORM):

foreach($obj as $o)
{
   echo $o->name;
   echo $o->email;
}

而不是使用当前,下一个等

3 个答案:

答案 0 :(得分:0)

  

PHP 5为对象提供了一种方法   定义所以可以迭代   通过项目列表,with,for   例如一个foreach声明。通过   默认,所有可见属性都将   用于迭代。

来源:http://www.php.net/manual/en/language.oop5.iterations.php

class MyClass
{
    public $var1 = 'value 1';
    public $var2 = 'value 2';
    public $var3 = 'value 3';

    protected $protected = 'protected var';
    private   $private   = 'private var';

    function iterateVisible() {
       echo "MyClass::iterateVisible:\n";
       foreach($this as $key => $value) {
           print "$key => $value\n";
       }
    }
}

$class = new MyClass();

foreach($class as $key => $value) {
    print "$key => $value\n";
}
echo "\n";


$class->iterateVisible();

<强>结果:

var1 => value 1
var2 => value 2
var3 => value 3

MyClass::iterateVisible:
var1 => value 1
var2 => value 2
var3 => value 3
protected => protected var
private => private var

答案 1 :(得分:0)

我非常喜欢独立的Doctrine ArrayCollection类:

代码:http://trac.doctrine-project.org/browser/trunk/lib/Doctrine/Common/Collections/ArrayCollection.php?rev=7481

API:http://www.doctrine-project.org/api/common/2.0/doctrine/common/collections/arraycollection.html


$objects = new Doctrine\Common\Collections\ArrayCollection();
$objects[] = new MyClass('name 1', 'email 1');
$objects[] = new MyClass('name 1', 'email 1');

foreach ($objects as $o)
{
    echo $o->name;
    echo $o->email;
}

答案 2 :(得分:0)

Execute()返回一个数组,并使用current()将结果集作为数组数组,读取kohana V3 API,我发现:

$users = DB::select()->from('users')->where('salesman', '=', TRUE)
         ->as_object()->execute();

foreach ($users as $user)
{
   echo $user->email;
}