Join the Stack Overflow Community
Stack Overflow is a community of 6.6 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

While using Codeigniter and its built-in class Mongo, is it possible to get objects instead of arrays?

I'd rather access documents via $doc->id than $doc['id']. Thanks.

share|improve this question
up vote 1 down vote accepted

The PHP MongoDB lib will always return result as array. If you wish to work with objects instead of arrays you will need to handle conversion yourself. As converting to an object is not as easy as casting type you would have to write your own function to convert it to an object.

You could do something in a sense

$obj = new stdClass();

foreach($mongoResult as $key => $val){
    $obj -> $key = $val;
}

Obviously this will work on basic result sets. You would need to write a more sophisticated function to handle more complex arrays.

share|improve this answer
    
This looks like too much pain in the butt to just work with objects for the sake of it. I'll stick to arrays then :-) Thanks! – StephaneMombuleau Mar 3 '14 at 19:32
    
I'd guess so :) good luck. – Rok Nemet Mar 3 '14 at 19:45

I believe the issue has to do with how you are querying the database.

$this->db->query()->get->result();

Will give you objects.

$this->db->query()->get()->result_array(); 

Will give you arrays

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.