public function testRelation1TOMany() { $user = new User(array("age" => 40, "name" => "John")); $user->save(); $user = User::one(); $id = $user->getId(); $this->assertInstanceOf("\\MongoId", $user->getId()); $book1 = new Book(); $book1->name = "book1"; $book1->save(); $book2 = new Book(); $book2->name = "book2"; $book2->save(); $this->assertInstanceOf("\\MongoId", $book1->getId()); $this->assertInstanceOf("\\MongoId", $book2->getId()); $user->books = Collection::make(array($book1, $book2)); $user->save(); $user = User::id($id); $books = $user->books; $this->assertEquals(2, $books->count()); $this->assertEquals("book1", $books->get((string) $book1->getId())->name); $this->assertEquals("book2", $books->get((string) $book2->getId())->name); }
public function testReferences() { $expected = 'field'; $user = new User(); $user->save(); $id = $user->getId(); $ref1 = new Book(); $ref1->fieldMappingRefs = $expected; $ref1->save(); $ref2 = new Book(); $ref2->fieldMappingRefs = $expected . '2'; $ref2->save(); $user->fieldMappingRefs = Collection::make(array($ref1, $ref2)); $user->save(); $user = User::id($id); $refs = $user->fieldMappingRefs; $this->assertEquals(2, $refs->count()); $this->assertEquals($expected, $refs->get((string) $ref1->getId())->fieldMappingRefs); $this->assertEquals($expected . '2', $refs->get((string) $ref2->getId())->fieldMappingRefs); $userRaw = self::$db->getDB()->{User::$collection}->findOne(array('_id' => $id)); $book1Raw = self::$db->getDB()->{Book::$collection}->findOne(array('_id' => $ref1->getId())); $book2Raw = self::$db->getDB()->{Book::$collection}->findOne(array('_id' => $ref2->getId())); $this->assertArrayHasKey('field_mapping_refs', $userRaw, 'Field `fieldMappingRefs` was not mapped correctly'); $this->assertArrayHasKey('$ref', $userRaw['field_mapping_refs'][0], 'Field `fieldMappingRefs.0` was not mapped correctly'); $this->assertArrayHasKey('$id', $userRaw['field_mapping_refs'][0], 'Field `fieldMappingRefs.0` was not mapped correctly'); $this->assertEquals(Book::$collection, $userRaw['field_mapping_refs'][0]['$ref'], 'Field `fieldMappingRef` was not mapped correctly'); $this->assertEquals($ref1->getId(), $userRaw['field_mapping_refs'][0]['$id'], 'Field `fieldMappingRef` was not mapped correctly'); $this->assertEquals($expected, $book1Raw['field_mapping_refs']); $this->assertArrayHasKey('$ref', $userRaw['field_mapping_refs'][1], 'Field `fieldMappingRefs.1` was not mapped correctly'); $this->assertArrayHasKey('$id', $userRaw['field_mapping_refs'][1], 'Field `fieldMappingRefs.1` was not mapped correctly'); $this->assertEquals(Book::$collection, $userRaw['field_mapping_refs'][1]['$ref'], 'Field `fieldMappingRef` was not mapped correctly'); $this->assertEquals($ref2->getId(), $userRaw['field_mapping_refs'][1]['$id'], 'Field `fieldMappingRef` was not mapped correctly'); $this->assertEquals($expected . '2', $book2Raw['field_mapping_refs']); }
protected function createBooksCollection(array $data) { $books = array(); foreach ($data as $item) { $books[] = $this->createBook($item); } return Collection::make($books); }
/** * If the attribute of $key is a reference , * load its original record from db and save to $_cache temporarily. * * @param string $key key * * @return null */ protected function loadRef($key) { $attrs = $this->getAttrs(); $reference = $attrs[$key]; $cache =& $this->_cache; if (isset($this->cleanData[$key])) { $value = $this->cleanData[$key]; } else { $value = null; } $model = $reference['model']; $type = $reference['type']; if (isset($cache[$key])) { $obj =& $cache[$key]; return $obj; } else { if (class_exists($model)) { if ($type == self::DATA_TYPE_REFERENCE) { if (\MongoDBRef::isRef($value)) { $object = $model::id($value['$id']); $cache[$key] = $object; return $object; } return null; } elseif ($type == self::DATA_TYPE_REFERENCES) { $res = array(); if (!empty($value)) { foreach ($value as $item) { if (isset($item['$id'], $item['$ref'])) { $record = $model::id($item['$id']); if ($record) { $res[] = $record; } } } } $set = Collection::make($res); $cache[$key] = $set; $obj =& $cache[$key]; return $obj; } } } }