2010-12-03 106 views
0

你能就如何解決以下錯誤提醒:的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'); 
     } 
    } 
+0

請顯示異常拋出的行。似乎不是控制器,而是視圖。 – biakaveron 2010-12-03 20:20:30

回答

0

你真的不需要as_array()那裏。 Database_Result對象的行爲默認爲數組,您可以在那裏執行foreach ($branches as $b) echo $b->id,甚至無需將其轉換爲數組;

Database_Result implements Countable, Iterator, SeekableIterator, ArrayAccess 

Database_Result的唯一當前使用:: as_array()方法是用於生成密鑰=> VAL陣列,我指出here。您目前無法將其轉換爲數據庫結果數組,但它seems logical at first

+0

感謝kemo修復它。用as_array完成 – drs 2010-12-06 13:37:38

0

我會試試這個:

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

它可能工作,有時你需要聲明它在你改變它之前。

相關問題