ErrorException [注意]:尝试获取非对象的属性

时间:2014-02-18 14:41:27

标签: php kohana kohana-3 kohana-orm

Controller.php这样

public function action_user() {
        $user_list = DB::select()->from('users')->execute();
//       print_r($user_list);
        $this->template->content = View::factory('user')
                                    ->bind('user_list',$user_list);
    }

当我在控制器中打印$user_list像这样print_r($user_list);我在数组中获取值。在视图中我试图使用for循环迭代并打印变量。但是我收到此错误“ErrorException [ Notice ]: Trying to get property of non-object”。如果我将变量打印为$user_list,则它将作为数组打印。

views.php

 <?php echo $user_list; ?> //printing array
    <?php
        foreach ($user_list as $user):
        echo $user->username;  //getting error here
        endforeach;
    ?>

1 个答案:

答案 0 :(得分:1)

使用as_object方法:

$user_list = DB::select()->from('zid_users')->as_object('User')->execute();

ORM

$user_list = ORM::factory('User')->find_all();