• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Cannot use object of type stdClass as array

#1
[eluser]xerosis[/eluser]
In my controller I am calling a model which takes data from a database:
Code:
$spec_info = $this->spec_model->GetSpecs(array("id" => $this->input->post('image_spec_id')), 'width, height');
            
echo $spec_info['width'];

This ends up with the error: "Cannot use object of type stdClass as array"

How am I supposed to write it so it works?

Below is the code for my spec_model
Code:
function GetSpecs($options = array(), $require = NULL)
    {
        
        // Rows needed
        if(isset($require))
        {
            $this->db->select($require);
        }
        
        // Qualification
        foreach (array('id', 'title', 'width', 'height', 'w_text_count', 'h_text_count', 'created_on', 'last_activity', 'status') as $field)
        {
               if (isset($options[$field])) $this->db->where($field, $options[$field]);
        }

        // limit / offset
        if(isset($options['limit']) && isset($options['offset']))
            $this->db->limit($options['limit'], $options['offset']);
        else if(isset($options['limit']))
            $this->db->limit($options['limit']);
            
        // sort
        if(isset($options['sortBy']) && isset($options['sortDirection']))
            $this->db->order_by($options['sortBy'], $options['sortDirection']);
            
        if(!isset($options['status'])) $this->db->where('status !=', 'deleted');
        
        $query = $this->db->get("spec");
        
        if(isset($options['count'])) return $query->num_rows();
        
        if(isset($options['id']))
            return $query->row(0);
            
        return $query->result();
    }

#2
[eluser]toopay[/eluser]
two option,

return the array instead object, on your model
Code:
return $query->result_array();
or on your controller
Code:
$spec_info = (array) $this->spec_model->GetSpecs(array("id" => $this->input->post('image_spec_id')), 'width, height');

#3
[eluser]toopay[/eluser]
For better understanding, you may want to read types in php : http://www.php.net/manual/en/language.types.php


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.