Kohana - ErrorException [Errore fatale]: Non è possibile utilizzare l'oggetto di tipo modello di serie
-
30-09-2019 - |
Domanda
Si può consigliare su come risolvere il seguente errore:
ErrorException [ Fatal Error ]: Cannot use object of type Model_Branch as array
Si prega di consultare il regolatore:
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');
}
}
Soluzione
Non hai veramente bisogno () lì. Database_Result oggetti si comportano come serie di default, si può fare foreach ($branches as $b) echo $b->id
lì senza nemmeno convertirlo in serie;
Database_Result implements Countable, Iterator, SeekableIterator, ArrayAccess
L'unico utilizzo corrente di Database_Result :: as_array () metodo sarebbe per generare chiave => val array, come ho sottolineato qui . Al momento non è possibile convertire questo alla serie di risultati del database, anche se sembra logico in un primo momento .
Altri suggerimenti
Vorrei provare questo:
$branches = $agent->branches->find_all();
$branches = $branches->as_array();
Potrebbe funzionare, a volte è necessario dichiararlo prima di trasformarla.