예제 #1
0
 public function action_document()
 {
     $this->out('Driver Version: ' . Mongo::VERSION);
     $this->test('INSERT Document WITHOUT _id');
     $data = array('name' => 'mongo', 'counter' => 10, 'set' => array('foo', 'bar', 'baz'), 'simplenested' => array('foo' => 'bar'), 'doublenested' => array('foo' => array('bar' => 'baz')));
     $this->out('BEFORE', $data);
     $doc = new Model_Document();
     $doc->load_values($data);
     $doc->save();
     $this->assert('document loaded after save', $doc->loaded() === TRUE);
     $this->out('AFTER', $doc->as_array());
     $this->assert('_id exists', $doc->id);
     $this->test('RETRIEVE DOCUMENT BY _id');
     $id = $doc->id;
     $doc = new Model_Document($id);
     $doc->load();
     $this->assert('document found', $doc->loaded() && $doc->name == 'mongo');
     $this->test('UPDATE Document');
     $doc->size = 'huge';
     $doc->save()->load();
     $this->assert('update saved', $doc->size == 'huge');
     $this->test('INCREMENT COUNTER');
     $old = $doc->counter;
     $doc->inc('counter')->save()->load();
     $this->assert('counter incremented', $old + 1 === $doc->counter);
     $this->test('UPSERT NON-EXISTING DOCUMENT');
     $doc = new Model_Document();
     $doc->name = 'Bugs Bunny';
     $doc->push('friends', 'Daffy Duck');
     $doc->upsert();
     $doc->load();
     $this->assert('document inserted on upsert', !empty($doc->id));
     $this->test('UPSERT EXISTING DOCUMENT');
     $doc = new Model_Document();
     $doc->name = 'Bugs Bunny';
     $doc->push('friends', 'Elmer Fudd');
     $doc->upsert();
     $doc->load();
     $this->assert('document updated on upsert', $doc->friends === array('Daffy Duck', 'Elmer Fudd'));
     $this->test('DELETE Document');
     $doc->delete();
     $doc->load(array('name' => 'Bugs Bunny'));
     $this->assert('document deleted', empty($doc->id));
     $this->test('INSERT Document WITH _id');
     $data = array('name' => 'mongo', 'counter' => 10, 'set' => array('foo', 'bar', 'baz'));
     $doc = new Model_Document();
     $doc->id = 'test_doc';
     $doc->load_values($data)->save();
     $doc = new Model_Document('test_doc');
     $doc->load();
     $this->assert('document found', $doc->loaded());
 }