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

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');
        }
    }
有帮助吗?

解决方案

您真的不需要那里的_array()。 database_result对象默认行为作为数组,您可以执行 foreach ($branches as $b) echo $b->id 那里甚至没有将其转换为数组;

Database_Result implements Countable, Iterator, SeekableIterator, ArrayAccess

如我指出的那样 这里. 。您目前无法将其转换为数据库结果,尽管 一开始似乎是合乎逻辑的.

其他提示

我会尝试的:

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

它可能起作用,有时您需要在改造它之前声明它。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top