Example #1
0
 public function testReference()
 {
     $doc = new Model_Test_Document();
     $doc->id = 'foo';
     $doc->other = Mongo_Document::factory('test_other');
     $doc->other->bar = 'baz';
     $doc->other->save();
     $doc->save();
     $this->assertNotNull($doc->_other, 'referenced document reference doesnt exist');
     $doc = new Model_Test_Document('foo');
     $this->assertEquals('baz', $doc->other->bar, 'nested document not saved');
     $docs = $doc->other->find_docs();
     $this->assertEquals(1, $docs->count(), 'doc not found');
     $doc0 = $docs->getNext();
     $this->assertEquals('foo', $doc0->id, 'doc id is expected');
     for ($i = 0; $i < 3; $i++) {
         $newdoc = Mongo_Document::factory('test_other')->load_values(array('id' => 'more' . $i, 'foo' => 'bar' . $i))->save();
         $doc->push('_lots', $newdoc->id);
     }
     $doc->save();
     $lots = $doc->lots;
     $this->assertEquals(3, $lots->count(), 'should find 3 referenced docs');
 }
Example #2
0
 /**
  * Instantiate an object conforming to Mongo_Collection conventions.
  *
  * @param   string  $name The model name to instantiate
  * @return  Mongo_Collection
  * @deprecated
  */
 public static function factory($name)
 {
     return Mongo_Document::factory($name)->collection(TRUE);
 }
Example #3
0
 public function action_reference()
 {
     $this->test('CREATE DOCUMENT WITH REFERENCED DOCUMENT');
     $doc = new Model_Document();
     $doc->id = 'foo';
     $doc->other = Mongo_Document::factory('other');
     $doc->other->bar = 'baz';
     $doc->other->save();
     $doc->save();
     $this->assert('referenced document reference exists', $doc->_other);
     $doc = new Model_Document('foo');
     $this->assert('nested document saved', $doc->other->bar == 'baz');
     $this->test('SEARCH DOCUMENTS BY PREDEFINED SEARCH');
     $docs = $doc->other->find_docs();
     $this->assert('1 docs found', $docs->count() == 1);
     $doc0 = $docs->getNext();
     $this->assert('doc id is expected', $doc0->id == 'foo');
     $this->test('LOAD MULTIPLE REFERENCED DOCUMENTS FROM ARRAY OF _ids');
     for ($i = 0; $i < 3; $i++) {
         $newdoc = Mongo_Document::factory('other')->load_values(array('id' => 'more' . $i, 'foo' => 'bar' . $i))->save();
         $doc->push('_lots', $newdoc->id);
     }
     $doc->save();
     $lots = $doc->lots;
     $this->assert('found 3 referenced docs', $lots->count() == 3);
 }
Example #4
0
 public function _unset($name, $emulate = true)
 {
     $this->_doc->_unset($this->get_field_name($name), $emulate);
     return $this;
 }
Example #5
0
 /**
  * Gets one of the following:
  *
  *  - A referenced object
  *  - A search() result
  *  - A field's value
  *
  * @param   string  $name field name
  * @return  mixed
  * @throws  \Exception
  */
 public function __get($name)
 {
     $name = $this->get_field_name($name, FALSE);
     // Auto-loading for special references
     if (array_key_exists($name, $this->_references)) {
         if (isset($this->_references[$name]['getter'])) {
             if ($this->_references[$name]['getter'] == null) {
                 throw new \Exception("'{$name}' is write only!");
             } else {
                 if (is_string($this->_references[$name]['getter'])) {
                     return call_user_func(array($this, $this->_references[$name]['getter']), $name);
                 } else {
                     return call_user_func(array($this, $this->_references[$name]['getter']), $this, $name);
                 }
             }
         }
         if (!isset($this->_related_objects[$name])) {
             $model = isset($this->_references[$name]['model']) ? $this->_references[$name]['model'] : $name;
             $foreign_field = isset($this->_references[$name]['foreign_field']) ? $this->_references[$name]['foreign_field'] : FALSE;
             if ($foreign_field) {
                 $this->_related_objects[$name] = Mongo_Document::factory($model)->collection(TRUE)->find($foreign_field, $this->id);
                 return $this->_related_objects[$name];
             }
             $id_field = isset($this->_references[$name]['field']) ? $this->_references[$name]['field'] : "_{$name}";
             $value = $this->__get($id_field);
             if (!empty($this->_references[$name]['multiple'])) {
                 $this->_related_objects[$name] = Mongo_Document::factory($model)->collection(TRUE)->find(array('_id' => array('$in' => (array) $value)));
             } else {
                 // Extract just id if value is a DBRef
                 if (is_array($value) && isset($value['$id'])) {
                     $value = $value['$id'];
                 }
                 $this->_related_objects[$name] = Mongo_Document::factory($model, $value);
             }
         }
         return $this->_related_objects[$name];
     }
     $this->load_if_needed($name);
     return isset($this->_object[$name]) ? $this->_object[$name] : NULL;
 }