/** * @return array */ public function jsonSerialize() { $output = array(); foreach (get_object_vars($this) as $key => $value) { if ($value instanceof \DateTime) { // Clumsy way to avoid writing mappings for sub-documents and all that. $output[$key] = Type::getType('datetime')->convertToCouchDBValue($value); } else { $output[$key] = $value; } } return array_filter($output); }
public function testGetTypesMap() { $this->assertArrayHasKey('mixed', Type::getTypesMap()); }
/** * Flush Operation - Write all dirty entries to the CouchDB. * * @return void */ public function flush() { $this->detectChangedDocuments(); if ($this->evm->hasListeners(Event::onFlush)) { $this->evm->dispatchEvent(Event::onFlush, new Event\OnFlushEventArgs($this)); } $config = $this->dm->getConfiguration(); $bulkUpdater = $this->dm->getCouchDBClient()->createBulkUpdater(); $bulkUpdater->setAllOrNothing($config->getAllOrNothingFlush()); foreach ($this->scheduledRemovals as $oid => $document) { $bulkUpdater->deleteDocument($this->documentIdentifiers[$oid], $this->documentRevisions[$oid]); $this->removeFromIdentityMap($document); if ($this->evm->hasListeners(Event::postRemove)) { $this->evm->dispatchEvent(Event::postRemove, new Event\LifecycleEventArgs($document, $this->dm)); } } foreach ($this->scheduledUpdates as $oid => $document) { $class = $this->dm->getClassMetadata(get_class($document)); if ($this->evm->hasListeners(Event::preUpdate)) { $this->evm->dispatchEvent(Event::preUpdate, new Event\LifecycleEventArgs($document, $this->dm)); $this->computeChangeSet($class, $document); // TODO: prevent association computations in this case? } $data = $this->metadataResolver->createDefaultDocumentStruct($class); // Convert field values to json values. foreach ($this->originalData[$oid] as $fieldName => $fieldValue) { if (isset($class->fieldMappings[$fieldName])) { if ($fieldValue !== null && isset($class->fieldMappings[$fieldName]['embedded'])) { // As we store the serialized value in originalEmbeddedData, we can simply copy here. $fieldValue = $this->originalEmbeddedData[$oid][$class->fieldMappings[$fieldName]['jsonName']]; } else { if ($fieldValue !== null) { $fieldValue = Type::getType($class->fieldMappings[$fieldName]['type'])->convertToCouchDBValue($fieldValue); } } $data[$class->fieldMappings[$fieldName]['jsonName']] = $fieldValue; } else { if (isset($class->associationsMappings[$fieldName])) { if ($class->associationsMappings[$fieldName]['type'] & ClassMetadata::TO_ONE) { if (\is_object($fieldValue)) { $fieldValue = $this->getDocumentIdentifier($fieldValue); } else { $fieldValue = null; } $data = $this->metadataResolver->storeAssociationField($data, $class, $this->dm, $fieldName, $fieldValue); } else { if ($class->associationsMappings[$fieldName]['type'] & ClassMetadata::TO_MANY) { if ($class->associationsMappings[$fieldName]['isOwning']) { // TODO: Optimize when not initialized yet! In ManyToMany case we can keep track of ALL ids $ids = array(); if (is_array($fieldValue) || $fieldValue instanceof \Doctrine\Common\Collections\Collection) { foreach ($fieldValue as $key => $relatedObject) { $ids[$key] = $this->getDocumentIdentifier($relatedObject); } } $data = $this->metadataResolver->storeAssociationField($data, $class, $this->dm, $fieldName, $ids); } } } } else { if ($class->hasAttachments && $fieldName == $class->attachmentField) { if (is_array($fieldValue) && $fieldValue) { $data['_attachments'] = array(); foreach ($fieldValue as $filename => $attachment) { if (!$attachment instanceof \Doctrine\CouchDB\Attachment) { throw CouchDBException::invalidAttachment($class->name, $this->documentIdentifiers[$oid], $filename); } $data['_attachments'][$filename] = $attachment->toArray(); } } } } } } // respect the non mapped data, otherwise they will be deleted. if (isset($this->nonMappedData[$oid]) && $this->nonMappedData[$oid]) { $data = array_merge($data, $this->nonMappedData[$oid]); } $rev = $this->getDocumentRevision($document); if ($rev) { $data['_rev'] = $rev; } $bulkUpdater->updateDocument($data); } $response = $bulkUpdater->execute(); $updateConflictDocuments = array(); if ($response->status == 201) { foreach ($response->body as $docResponse) { if (!isset($this->identityMap[$docResponse['id']])) { // deletions continue; } $document = $this->identityMap[$docResponse['id']]; if (isset($docResponse['error'])) { $updateConflictDocuments[] = $document; } else { $this->documentRevisions[spl_object_hash($document)] = $docResponse['rev']; $class = $this->dm->getClassMetadata(get_class($document)); if ($class->isVersioned) { $class->reflFields[$class->versionField]->setValue($document, $docResponse['rev']); } } if ($this->evm->hasListeners(Event::postUpdate)) { $this->evm->dispatchEvent(Event::postUpdate, new Event\LifecycleEventArgs($document, $this->dm)); } } } else { if ($response->status >= 400) { throw HTTPException::fromResponse($bulkUpdater->getPath(), $response); } } foreach ($this->visitedCollections as $col) { $col->takeSnapshot(); } $this->scheduledUpdates = $this->scheduledRemovals = $this->visitedCollections = array(); if (count($updateConflictDocuments)) { throw new UpdateConflictException($updateConflictDocuments); } }
/** * Compares the two representation of an embedded document. * * If the original misses doctrine_metadata, but the values are the same, we assume there is no change * If the original has doctrine_metadata, and the new value has different class, that's a change, * even if the values are the same. * * @param array $value * @param object $originalData * @param array $fieldMapping Mapping of the field that contains the embedded document in the * embedder document. * @return boolean */ public function isChanged($value, $originalData, $valueFieldMapping) { // EmbedMany case if ('many' == $valueFieldMapping['embedded'] && is_array($value)) { if (count($originalData) != count($value)) { return true; } foreach ($value as $key => $valueElement) { if (!isset($originalData[$key]) || $this->isChanged($valueElement, $originalData[$key], $valueFieldMapping)) { return true; } } return false; } // EmbedOne case, or one instance of and EmbedMany if ($this->metadataResolver->canMapDocument($originalData) && get_class($value) != $this->metadataResolver->getDocumentType($originalData)) { return true; } $class = $this->metadataFactory->getMetadataFor(get_class($value)); foreach ($class->reflFields as $fieldName => $fieldValue) { $fieldMapping = $class->fieldMappings[$fieldName]; $originalDataValue = isset($originalData[$fieldMapping['jsonName']]) ? $originalData[$fieldMapping['jsonName']] : null; $currentValue = $class->getFieldValue($value, $fieldMapping['fieldName']); if ($originalDataValue == null && $currentValue == null) { continue; } else { if ($originalDataValue == null || $currentValue == null) { return true; } } if (!isset($fieldMapping['embedded'])) { // simple property comparison // TODO this conversion could be avoided if we store the php value in the original data // as with the simple property mapping in UOW. $originalValue = Type::getType($fieldMapping['type'])->convertToPHPValue($originalDataValue); if ($originalValue != $currentValue) { return true; } } else { if ('many' == $fieldMapping['embedded']) { if (count($originalDataValue) != count($currentValue)) { return true; } foreach ($currentValue as $currentKey => $currentElem) { if (!isset($originalDataValue[$currentKey])) { return true; } if ($this->isChanged($currentElem, $originalDataValue[$currentKey], $fieldMapping)) { return true; } } } else { // embedOne if ($this->isChanged($currentValue, $originalDataValue, $fieldMapping)) { return true; } } } } return false; }
public function setUp() { $this->type = Type::getType('datetime'); }
public function setUp() { $this->type = Type::getType('mixed'); }