KOHANA - ErrorException [ Fatal Error ]: Cannot use object of type Model as array
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');
}
}
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.
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.
精彩评论