public function testLoadingMappedsuperclass() { $document = new ExtendingClass(); $document->topic = 'Superclass test'; $document->headline = 'test test test'; $this->dm->persist($document); $this->dm->flush(); $id = $document->id; $this->dm->clear(); $doc = $this->dm->find('Doctrine\\Tests\\Models\\Mapping\\ExtendingClass', $id); $this->assertInstanceOf('\\Doctrine\\Tests\\Models\\Mapping\\ExtendingClass', $doc); $this->assertEquals('test test test', $doc->headline); $this->assertEquals('Superclass test', $doc->topic); }
public function execute() { $response = $this->doExecute(); if ($this->dm && $this->getParameter('include_docs') === true) { $uow = $this->dm->getUnitOfWork(); foreach ($response->body['rows'] as $k => $v) { if (!isset($v['type']) && !$this->documentName) { throw new \InvalidArgumentException("Cannot query " . $this->getHttpQuery() . " lucene and convert to document instances, " . "the type of document " . $v['id'] . " is not stored in Lucene. You can query without " . "include_docs and pass the ids to findMany() of the repository you know this document is " . "a type of."); } $v['type'] = isset($v['type']) ? $v['type'] : $this->documentName; $doc = $this->dm->find(str_replace(".", "\\", $v['type']), $v['id']); if ($this->onlyDocs) { $response->body['rows'][$k] = $doc; } else { $response->body['rows'][$k]['doc'] = $doc; } } } return $this->createResult($response); }
/** * Fetch an object from persistence layer. * * @param string $identity * @param string $targetType * @return object * @throws \TYPO3\Flow\Property\Exception\TargetNotFoundException * @throws \TYPO3\Flow\Property\Exception\InvalidSourceException */ protected function fetchObjectFromPersistence($identity, $targetType) { if (is_string($identity)) { $object = $this->documentManager->find($targetType, $identity); } else { throw new \TYPO3\Flow\Property\Exception\InvalidSourceException('The identity property "' . $identity . '" is not a string.', 1356681336); } if ($object === NULL) { throw new \TYPO3\Flow\Property\Exception\TargetNotFoundException('Document with identity "' . print_r($identity, TRUE) . '" not found.', 1356681356); } return $object; }
/** * @Flow\Around("within(TYPO3\Flow\Persistence\PersistenceManagerInterface) && method(.*->getObjectByIdentifier())") * @param \TYPO3\Flow\Aop\JoinPointInterface $joinPoint The current join point * @return object */ public function getObjectByIdentifier(\TYPO3\Flow\Aop\JoinPointInterface $joinPoint) { $object = $joinPoint->getAdviceChain()->proceed($joinPoint); if ($object === NULL) { try { $document = $this->documentManager->find($joinPoint->getMethodArgument('objectType'), $joinPoint->getMethodArgument('identifier')); if ($document !== NULL) { return $document; } } catch (\Doctrine\ODM\CouchDB\Mapping\MappingException $exception) { // probably not a valid document, so ignore it } } return $object; }
private function doMerge($document, array &$visited, $prevManagedCopy = null, $assoc = null) { $oid = spl_object_hash($document); if (isset($visited[$oid])) { return; // Prevent infinite recursion } $visited[$oid] = $document; // mark visited $class = $this->dm->getClassMetadata(get_class($document)); // First we assume DETACHED, although it can still be NEW but we can avoid // an extra db-roundtrip this way. If it is not MANAGED but has an identity, // we need to fetch it from the db anyway in order to merge. // MANAGED entities are ignored by the merge operation. if ($this->getDocumentState($document) == self::STATE_MANAGED) { $managedCopy = $document; } else { $id = $class->getIdentifierValue($document); if (!$id) { // document is new // TODO: prePersist will be fired on the empty object?! $managedCopy = $class->newInstance(); $this->persistNew($class, $managedCopy); } else { $managedCopy = $this->tryGetById($id); if ($managedCopy) { // We have the document in-memory already, just make sure its not removed. if ($this->getDocumentState($managedCopy) == self::STATE_REMOVED) { throw new \InvalidArgumentException('Removed document detected during merge.' . ' Can not merge with a removed document.'); } } else { // We need to fetch the managed copy in order to merge. $managedCopy = $this->dm->find($class->name, $id); } if ($managedCopy === null) { // If the identifier is ASSIGNED, it is NEW, otherwise an error // since the managed document was not found. if ($class->idGenerator == ClassMetadata::IDGENERATOR_ASSIGNED) { $managedCopy = $class->newInstance(); $class->setIdentifierValue($managedCopy, $id); $this->persistNew($class, $managedCopy); } else { throw new DocumentNotFoundException(); } } } if ($class->isVersioned) { $managedCopyVersion = $class->reflFields[$class->versionField]->getValue($managedCopy); $documentVersion = $class->reflFields[$class->versionField]->getValue($document); // Throw exception if versions dont match. if ($managedCopyVersion != $documentVersion) { throw OptimisticLockException::lockFailedVersionMissmatch($document, $documentVersion, $managedCopyVersion); } } $managedOid = spl_object_hash($managedCopy); // Merge state of $entity into existing (managed) entity foreach ($class->reflFields as $name => $prop) { if (!isset($class->associationsMappings[$name])) { if (!$class->isIdentifier($name)) { $prop->setValue($managedCopy, $prop->getValue($document)); } } else { $assoc2 = $class->associationsMappings[$name]; if ($assoc2['type'] & ClassMetadata::TO_ONE) { $other = $prop->getValue($document); if ($other === null) { $prop->setValue($managedCopy, null); } else { if ($other instanceof Proxy && !$other->__isInitialized__) { // do not merge fields marked lazy that have not been fetched. continue; } else { if ($assoc2['cascade'] & ClassMetadata::CASCADE_MERGE == 0) { if ($this->getDocumentState($other) == self::STATE_MANAGED) { $prop->setValue($managedCopy, $other); } else { $targetClass = $this->dm->getClassMetadata($assoc2['targetDocument']); $id = $targetClass->getIdentifierValues($other); $proxy = $this->dm->getProxyFactory()->getProxy($assoc2['targetDocument'], $id); $prop->setValue($managedCopy, $proxy); $this->registerManaged($proxy, $id, null); } } } } } else { $mergeCol = $prop->getValue($document); if ($mergeCol instanceof PersistentCollection && !$mergeCol->isInitialized) { // do not merge fields marked lazy that have not been fetched. // keep the lazy persistent collection of the managed copy. continue; } $managedCol = $prop->getValue($managedCopy); if (!$managedCol) { if ($assoc2['isOwning']) { $managedCol = new PersistentIdsCollection(new ArrayCollection(), $assoc2['targetDocument'], $this->dm, array()); } else { $managedCol = new PersistentViewCollection(new ArrayCollection(), $this->dm, $this->documentIdentifiers[$managedOid], $assoc2); } $prop->setValue($managedCopy, $managedCol); $this->originalData[$managedOid][$name] = $managedCol; } if ($assoc2['cascade'] & ClassMetadata::CASCADE_MERGE > 0) { $managedCol->initialize(); if (!$managedCol->isEmpty()) { // clear managed collection, in casacadeMerge() the collection is filled again. $managedCol->unwrap()->clear(); $managedCol->setDirty(true); } } } } } } if ($prevManagedCopy !== null) { $assocField = $assoc['fieldName']; $prevClass = $this->dm->getClassMetadata(get_class($prevManagedCopy)); if ($assoc['type'] & ClassMetadata::TO_ONE) { $prevClass->reflFields[$assocField]->setValue($prevManagedCopy, $managedCopy); } else { $prevClass->reflFields[$assocField]->getValue($prevManagedCopy)->add($managedCopy); if ($assoc['type'] == ClassMetadata::ONE_TO_MANY) { $class->reflFields[$assoc['mappedBy']]->setValue($managedCopy, $prevManagedCopy); } } } // Mark the managed copy visited as well $visited[spl_object_hash($managedCopy)] = true; $this->cascadeMerge($document, $managedCopy, $visited); return $managedCopy; }