Kohana -errorexception [致命的なエラー]:タイプモデルのオブジェクトを配列として使用できません

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

  •  30-09-2019
  •  | 
  •  

質問

次のエラーを解決する方法についてアドバイスできますか?

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');
        }
    }
役に立ちましたか?

解決

そこにas_array()は本当に必要ありません。 database_resultオブジェクトはデフォルトで配列として動作します、あなたはすることができます foreach ($branches as $b) echo $b->id それを配列に変換することさえありません。

Database_Result implements Countable, Iterator, SeekableIterator, ArrayAccess

Database_Result :: as_array()の唯一の現在の使用法は、私が指摘したように、key => valアレイを生成するためのものです。 ここ. 。現在、これはデータベースの結果の配列に変換することはできませんが、 最初は論理的です.

他のヒント

私はこれを試してみます:

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

それは機能するかもしれません、時にはあなたがそれを変換する前にそれを宣言する必要があります。

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top