4

I have a php object that I would like to store in my Mongo database. What is the best way to store the object in the database? I was thinking of looping over the object and creating an array but this is a complex object that has sub objects as well. Thanks

2
  • 3
    can you show how your object look like Commented Jun 19, 2011 at 21:03
  • As what do you want your object to store? Do you want to access it's properties via mongo selectivly? Commented Jun 19, 2011 at 21:08

3 Answers 3

9

The easiest way is probably to make your object "castable" to an array.

If the properties you want to store are public, you can just do:

$array = (array)$foo;

Otherwise, a toArray method, or making it implement an Iterator interface will work:

class Foo implements IteratorAggregate {

   protected $bar = 'hello';

   protected $baz = 'world';

   public function getIterator() {
       return new ArrayIterator(array(
           'bar' => $this->bar,
           'baz' => $this->baz,
       ));
   }

}

Obviously, you can also use get_object_vars, Reflection and such instead of hardcoding the property list in the getIterator method.

Then, just:

$foo = new Foo;
$array = iterator_to_array($foo);
$mongodb->selectCollection('Foo')->insert($array);

Depending on how you want to store your objects, you may want to use DBRefs instead of storing nested objects all at once, so you can easily find them separately afterwards. If not, just make your toArray method recursive.

Sign up to request clarification or add additional context in comments.

5 Comments

This seems like a lot of effort if all you're trying to do is persist an object. Assuming that's all you need, the other answer (serialize/unserialize) is the way to go. PHP will automatically return the object with the appropriate type when you call unserialize(), which is pretty nice.
@beamrider9: Fact is, MongoDB is a lot better suited to store objects and render them searchable field-by-field than a RDBMS. Not taking advantage of this would be a mistake in my opinion. If all one wants is to persist objects, Sqlite can do that too.
Someone asked "Do you want to access it's properties via mongo selectively?" and the OP didn't answer - you're assuming the answer's "yes", which it may be but you don't know. I've just wanted to store an object plenty of times without needing to search on individual fields. And good luck scaling your website built on top of sqlite-persisted objects. Hey, why not use MS Access?
Scaling a website? Now you're the one assuming.
One last solution: you could store its var_export, and eval to get back to the object.
1

encode to JSON and insert to MongoDB.

Comments

0

If you want save your value into Mongo Object ID:

$param = 'ojkhalskdjfhs9df87as08df';
$this->insert($collection, [
'aaaaa' => new MongoId($param)
]);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.