Question

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');
        }
    }
Was it helpful?

Solution

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.

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top