Welcome Guest, Not a member yet? Register   Sign In
Postgres 9.2 Upgrade Broke Foreach on ->result()
#1

[eluser]Doulos[/eluser]
Just upgraded my DB to Postgres 9.2 to get Index only Counts but now, getting a ->result() from a database query can't be Looped Through with a foreach.

Code:
function get_provider_address_by_id($id){
        $this->db->select('id');
        $this->db->select('address1');
        $this->db->select('address2');
        $this->db->select('city');
        $this->db->select('state');
        $this->db->select('zip');
        $this->db->from('list_address');
        $this->db->where('provider_id', $id);
        $addresses = $this->db->get();
        $addresses = $addresses->result();
        echo var_dump($addresses);
        foreach($addresses as $key=>$address){
            //Get how many times it's used
            $address->uses = $this->countAddressUses($address->id);
            $addresses[$key]=$address;
        }

        return $addresses;
    }

Results in

Quote:A PHP Error was encountered

Severity: Warning

Message: Invalid argument supplied for foreach()

Filename: database/DB_active_rec.php

Line Number: 82

Any Ideas? There's something funky with the Driver Somewhere. Previously, not only did this work, but the object didn't have 'public' or '$' associated with each name.


Edit: Should also add, the below does the same thing.

Code:
function get_provider_address_by_id($id){
        $this->db->select('id');
        $this->db->select('address1');
        $this->db->select('address2');
        $this->db->select('city');
        $this->db->select('state');
        $this->db->select('zip');
        $this->db->from('list_address');
        $this->db->where('provider_id', $id);
        $addresses = $this->db->get();
        //$addresses = $addresses->result();
        //echo var_dump($addresses);
        foreach($addresses->result() as $key=>$address){
            //Get how many times it's used
            $address->uses = $this->countAddressUses($address->id);
            $addresses[$key]=$address;
        }

        return $addresses;
    }
#2

[eluser]Doulos[/eluser]
Not sure this won't have other consequences, so people more familiar with the code base can do the modification and pull request,

But the fix I used is to update the public function select in DB_active_rec.php

Code:
public function select($select = '*', $escape = NULL)
{
  if (is_string($select))
  {
   $select = explode(',', $select);
  }
        //Added the Is_array Check 11/6/2012
        if (is_array($select)){
            foreach ($select as $val)
            {
                $val = trim($val);

                if ($val != '')
                {
                    $this->ar_select[] = $val;
                    $this->ar_no_escape[] = $escape;

                    if ($this->ar_caching === TRUE)
                    {
                        $this->ar_cache_select[] = $val;
                        $this->ar_cache_exists[] = 'select';
                        $this->ar_cache_no_escape[] = $escape;
                    }
                }
            }
        }
  return $this;
}
#3

[eluser]Doulos[/eluser]
Ignore me, there was a $this->db->select(count('id')) in my sub function that was breaking things.

Quotes help kids!




Theme © iAndrew 2016 - Forum software by © MyBB