KOHANA - ErrorException [致命错误]:无法将模型类型的对象用作数组
您能否建议如何解决以下错误:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你真的不需要 as_array() 那里。 Database_Result 对象默认表现为数组,您可以在那里执行
foreach ($branches as $b) echo $b->id
,甚至无需将其转换为数组;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;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.
我会尝试这个:
它可能会起作用,有时您需要在转换它之前声明它。
I would try this:
It might work, sometimes you need to declare it before you transform it.