0

次のエラーの解決方法を教えてください。

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');
        }
    }
4

2 に答える 2

0

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

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

うまくいくかもしれませんが、変換する前に宣言する必要がある場合があります。

于 2010-12-03T17:49:58.443 に答える
0

そこに as_array() は本当に必要ありません。Database_Result オブジェクトはデフォルトで配列として動作しますforeach ($branches as $b) echo $b->id。配列に変換しなくても実行できます。

Database_Result implements Countable, Iterator, SeekableIterator, ArrayAccess

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

于 2010-12-03T18:54:30.030 に答える