KOHANA - ErrorException [ Fatal Error ]: Cannot use object of type Model as array

StackOverflow https://stackoverflow.com/questions/4347589

  •  30-09-2019
  •  | 
  •  

Pergunta

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');
        }
    }
Foi útil?

Solução

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.

Outras dicas

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.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top