EllisLab text mark
Advanced Search
     
Postgres 9.2 Upgrade Broke Foreach on ->result()
Posted: 06 November 2012 03:54 PM   [ Ignore ]
Joined: 2011-06-15
15 posts

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.

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

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.

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;
    

 

 
Posted: 06 November 2012 04:01 PM   [ Ignore ]   [ # 1 ]   [ Rating: 0 ]
Joined: 2011-06-15
15 posts

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

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;
 
 
Posted: 06 November 2012 04:44 PM   [ Ignore ]   [ # 2 ]   [ Rating: 0 ]
Joined: 2011-06-15
15 posts

Ignore me, there was a $this->db->select(count(‘id’)) in my sub function that was breaking things.

Quotes help kids!