public function testShouldCreateDBRefFromCollection() { $collection = new Product(); $collection->setName('Test'); $collection->save(); $dbRef = DbRef::create($collection->getSource(), $collection); $this->assertTrue(\MongoDbRef::isRef($dbRef)); $this->assertEquals($collection->getId(), $dbRef['$id']); $this->assertEquals($collection->getSource(), $dbRef['$ref']); }
public function testExportReferences() { $schema = new Test_Document_Mongo_Schema(); $test = array('test' => true); $testRef = $schema->resolve('doc:test'); $testRef->test = true; $testRef->save(); $brokenRef = array('$ref' => 'test_document', '$id' => 'non_exist'); $testRef = $testRef->createReference(); $data = array('testDoc' => $test, 'testArray' => array('test' => 'test'), 'testSet' => array($testRef, $brokenRef), 'testProp' => $testRef, 'testMulti' => $brokenRef); $doc = $schema->resolve('doc:testRequirements', $data); $this->assertTrue($doc->testSet[0]->isRootDocument()); $this->assertEquals(null, $doc->testSet[1]); $this->assertEquals("clean", $doc->isReference('testProp')); $this->assertInstanceOf('Epic_Mongo_Document', $doc->testProp); $this->assertTrue($doc->testProp->isRootDocument()); $this->assertEquals("data", $doc->isReference('testProp')); $this->assertInstanceOf('Epic_Mongo_Document', $doc->testMulti); $export = $doc->export(); $this->assertTrue(MongoDbRef::isRef($export['testSet'][0])); $this->assertEquals("test", $export["testArray"]["test"]); }
public function doc($key, $data = null) { if (array_key_exists($key, $this->_data)) { return $this->_data[$key]->extend($data); } if (!is_array($data)) { $data = array(); } $set = $this->hasRequirement($key, 'set'); $doc = $this->hasRequirement($key, 'doc'); $ref = MongoDbRef::isRef($data); $config = array(); if ($ref) { $config['collection'] = $data['$ref']; $config['isReference'] = true; $data = MongoDBRef::get($this->getSchema()->getMongoDB(), $data); // If this is a broken reference then no point keeping it for later if (!$data) { if ($this->hasRequirement($key, 'auto')) { $data = array(); } else { return $this->_data[$key] = null; } } } if (!($doc || $set)) { $set = $this->_dataIsSimpleArray($data); } $schemaType = $set ? 'set' : 'doc'; if ($documentClass = $this->getRequirement($key, $schemaType)) { $schemaType .= ":" . $documentClass; } $doc = $this->getSchema()->resolve($schemaType, $data, $config); $this->setProperty($key, $doc); return $doc; }