/** * @test */ public function getDocumentChangeSet_includes_only_changes_for_update() { $uow = $this->manager->getUnitOfWork(); $c = new Contact(); $c->rid = "#1:1"; $c->name = "Sydney"; $c->height = 5; $c->birthday = null; $uow->registerManaged($c, "#1:1", ['rid' => '#1:1', 'name' => 'Sydney', 'height' => null, 'birthday' => null]); $md = $this->manager->getClassMetadata(Contact::class); $uow->computeChangeSet($md, $c); $cs = $uow->getDocumentChangeSet($c); $this->assertEquals(['height'], array_keys($cs)); $this->assertEquals([null, 5], $cs['height']); }
/** * @test */ public function hydrate_contact_with_link_list() { $md = $this->manager->getClassMetadata(Stub\Linked\Contact::class); $dh = new DynamicHydrator($this->manager, $this->manager->getUnitOfWork(), $md); $c = new Stub\Linked\Contact(); $d = json_decode(<<<JSON { "@rid": "#1:1", "name": "Sydney", "phones": [ "#3:1", "#3:2" ] } JSON , true); $hd = $dh->hydrate($c, $d); $this->assertEquals(['rid', 'name', 'phones'], array_keys($hd)); $this->assertEquals('#1:1', $hd['rid']); $this->assertEquals('Sydney', $hd['name']); /** @var Stub\Linked\Phone[] $phones */ $phones = $c->getPhones()->toArray(); $this->assertCount(2, $phones); $phone = $phones[0]; $this->assertEquals('work', $phone->type); $this->assertEquals(UnitOfWork::STATE_MANAGED, $this->uow->getDocumentState($phone)); $this->assertTrue($this->uow->isInIdentityMap($phone)); $phone = $phones[1]; $this->assertEquals('home', $phone->type); $this->assertEquals(UnitOfWork::STATE_MANAGED, $this->uow->getDocumentState($phone)); $this->assertTrue($this->uow->isInIdentityMap($phone)); }
private function gatherProperties(ClassMetadata $class, OClass $oclass) { foreach ($class->fieldMappings as $mapping) { if (isset($mapping['inherited'])) { continue; } $name = $mapping['name']; if (OSchema::isSystemProperty($name)) { continue; } if (isset($mapping['association'])) { continue; } $options = ['readonly' => $mapping['readonly'], 'mandatory' => $mapping['mandatory'], 'min' => $mapping['min'], 'max' => $mapping['max'], 'regexp' => $mapping['regexp'], 'notNull' => !$mapping['nullable']]; $oclass->addProperty($name, $mapping['type'], $options); } foreach ($class->associationMappings as $mapping) { if (isset($mapping['inherited'])) { continue; } if (isset($mapping['direction'])) { // no in / outgoing references continue; } $name = $mapping['name']; if (isset($mapping['targetDoc'])) { $linkedClass = $this->_dm->getClassMetadata($mapping['targetDoc'])->orientClass; } else { $linkedClass = null; } $options = ['notNull' => !$mapping['nullable'], 'linkedClass' => $linkedClass]; $oclass->addProperty($name, $mapping['type'], $options); } }
/** * Creates a connection to the test database, if there is none yet, and * creates the necessary tables. */ protected function setUp() { if (!$this->dm) { $this->dm = $this->createDocumentManager(['binding' => self::$_b]); $this->schemaTool = new SchemaTool($this->dm); } $prime = []; $classes = []; foreach ($this->usedModelSets as $setName => $bool) { foreach (static::$_modelSets[$setName] as $className) { $prime[$setName][] = $this->dm->getClassMetadata($className); } if (!isset(static::$_classesCreated[$setName])) { $classes = array_merge($classes, $prime[$setName]); static::$_classesCreated[$setName] = true; } } if ($classes) { $this->schemaTool->createSchema($classes); } }
/** * @test */ public function getDocumentChangeSet_includes_embedded_list_for_new() { $uow = $this->manager->getUnitOfWork(); $c = new Contact(); $this->manager->persist($c); $c->name = "Sydney"; $phone = new Phone(); $phone->type = "work"; $phone->phoneNumber = "4804441919"; $c->phones[] = $phone; $md = $this->manager->getClassMetadata(Contact::class); $uow->computeChangeSet($md, $c); $cs = $uow->getDocumentChangeSet($c); $this->assertEquals(['name', 'email', 'phones'], array_keys($cs)); }
/** * @inheritdoc */ public function hydrate($document, $data, array $hints = []) { $metadata = $this->dm->getClassMetadata(get_class($document)); // Invoke preLoad lifecycle events and listeners // if ( ! empty($metadata->lifecycleCallbacks[Events::preLoad])) { // $args = array(&$data); // $metadata->invokeLifecycleCallbacks(Events::preLoad, $document, $args); // } // if ($this->evm->hasListeners(Events::preLoad)) { // $this->evm->dispatchEvent(Events::preLoad, new PreLoadEventArgs($document, $this->dm, $data)); // } $data = $this->getHydratorFor($metadata->name)->hydrate($document, $data, $hints); if ($document instanceof Proxy) { $document->__isInitialized__ = true; } // Invoke the postLoad lifecycle callbacks and listeners // if ( ! empty($metadata->lifecycleCallbacks[Events::postLoad])) { // $metadata->invokeLifecycleCallbacks(Events::postLoad, $document); // } // if ($this->evm->hasListeners(Events::postLoad)) { // $this->evm->dispatchEvent(Events::postLoad, new LifecycleEventArgs($document, $this->dm)); // } return $data; }
/** * INTERNAL: * Removes a document from the identity map. This effectively detaches the * document from the persistence management of Doctrine. * * @ignore * * @param object $document * * @return boolean */ public function removeFromIdentityMap($document) { $oid = spl_object_hash($document); // Check if id is registered first if (!isset($this->documentIdentifiers[$oid])) { return false; } $class = $this->dm->getClassMetadata(get_class($document)); if ($class->isEmbeddedDocument()) { $id = spl_object_hash($document); } else { $id = $this->documentIdentifiers[spl_object_hash($document)]; } if (isset($this->identityMap[$class->name][$id])) { unset($this->identityMap[$class->name][$id]); $this->documentStates[$oid] = self::STATE_DETACHED; return true; } return false; }
private function loadEmbedCollection(PersistentCollection $collection) { $data = $collection->getData(); if (count($data) === 0) { return; } if (is_array($data)) { $mapping = $collection->getMapping(); $useKey = boolval($mapping['association'] & ClassMetadata::ASSOCIATION_USE_KEY); $metadata = $this->dm->getClassMetadata($mapping['targetDoc']); foreach ($data as $key => $v) { $document = $metadata->newInstance(); $data = $this->hydratorFactory->hydrate($document, $v); $this->uow->registerManaged($document, null, $data); if ($useKey) { $collection->set($key, $document); } else { $collection->add($document); } } } }