KOHANA - ErrorException [致命错误]:无法将模型类型的对象用作数组

发布于 2024-10-05 16:34:41 字数 846 浏览 2 评论 0原文

您能否建议如何解决以下错误:

ErrorException [ Fatal Error ]: Cannot use object of type Model_Branch as array

请参阅控制器:


public function action_view($agent_id='') {
        $agent =  ORM::factory('agent', $agent_id);

        if ($agent->loaded()) {

            $values = $agent->as_array();
            $branches = $agent->branches->find_all()->as_array();

            // Show page
            $this->template->title = $agent->company_name;
            $this->template->content = View::factory('agent/view')
                    ->bind('title', $this->template->title)
                    ->bind('values', $values)
                    ->bind('branches', $branches);
        } else {
            Request::instance()->redirect('agent');
        }
    }

Can you advise on how to resolve the following error:

ErrorException [ Fatal Error ]: Cannot use object of type Model_Branch as array

Please see controller:


public function action_view($agent_id='') {
        $agent =  ORM::factory('agent', $agent_id);

        if ($agent->loaded()) {

            $values = $agent->as_array();
            $branches = $agent->branches->find_all()->as_array();

            // Show page
            $this->template->title = $agent->company_name;
            $this->template->content = View::factory('agent/view')
                    ->bind('title', $this->template->title)
                    ->bind('values', $values)
                    ->bind('branches', $branches);
        } else {
            Request::instance()->redirect('agent');
        }
    }

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

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

发布评论

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

评论(2

瞎闹 2024-10-12 16:34:41

你真的不需要 as_array() 那里。 Database_Result 对象默认表现为数组,您可以在那里执行 foreach ($branches as $b) echo $b->id ,甚至无需将其转换为数组;

Database_Result implements Countable, Iterator, SeekableIterator, ArrayAccess

Database_Result::as_array() 方法当前唯一的用途是生成 key => val 数组,正如我在此处指出的那样。目前您无法将其转换为数据库结果数组,尽管它乍一看似乎合乎逻辑

You don't really need as_array() there. Database_Result objects behave as array by default, you can do foreach ($branches as $b) echo $b->id there without even converting it to array;

Database_Result implements Countable, Iterator, SeekableIterator, ArrayAccess

The only current usage of Database_Result::as_array() method would be for generating key => val arrays, as I pointed out here. You currently can't convert this to array of database results, although it seems logical at first.

眼眸里的快感 2024-10-12 16:34:41

我会尝试这个:

$branches = $agent->branches->find_all();
$branches = $branches->as_array();

它可能会起作用,有时您需要在转换它之前声明它。

I would try this:

$branches = $agent->branches->find_all();
$branches = $branches->as_array();

It might work, sometimes you need to declare it before you transform it.

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