Kohanaphp v3 执行()

发布于 2024-09-11 20:52:03 字数 215 浏览 6 评论 0原文

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

我怎样才能获得一个对象(带有对象)来像这样迭代(没有ORM):

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

而不是使用当前、下一个等。

excecute() method always return an "informational" object after use current() method i get an array always.

How can i obtain an object (with objects) to iterate like this, (no ORM):

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

Instead of using current, next, etc.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

莳間冲淡了誓言ζ 2024-09-18 20:52:03

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

PHP 5 provides a way for objects to be
defined so it is possible to iterate
through a list of items, with, for
example a foreach statement. By
default, all visible properties will
be used for the iteration.

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

Result:

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
嗼ふ静 2024-09-18 20:52:03

我真的很喜欢独立的 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/学说/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;
}

I really like the standalone Doctrine ArrayCollection class:

Code: 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;
}
余生共白头 2024-09-18 20:52:03

Execute() 返回一个数组,并使用 current() 您将结果集作为数组的数组获得,阅读 kohana V3 API,我发现了这一点:

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

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

Execute() returns an array, and using current() you get the result set as an array of arrays, reading kohana V3 API, I found this:

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

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