/** * Creates a new Document that operates on the given Mongo connection * and uses the given Configuration. * * @param \Doctrine\MongoDB\Connection|null $conn * @param Configuration|null $config * @param \Doctrine\Common\EventManager|null $eventManager */ protected function __construct(Connection $conn = null, Configuration $config = null, EventManager $eventManager = null) { $this->config = $config ?: new Configuration(); $this->eventManager = $eventManager ?: new EventManager(); $this->connection = $conn ?: new Connection(null, array(), $this->config, $this->eventManager); $metadataFactoryClassName = $this->config->getClassMetadataFactoryName(); $this->metadataFactory = new $metadataFactoryClassName(); $this->metadataFactory->setDocumentManager($this); $this->metadataFactory->setConfiguration($this->config); if ($cacheDriver = $this->config->getMetadataCacheImpl()) { $this->metadataFactory->setCacheDriver($cacheDriver); } $hydratorDir = $this->config->getHydratorDir(); $hydratorNs = $this->config->getHydratorNamespace(); $this->hydratorFactory = new HydratorFactory($this, $this->eventManager, $hydratorDir, $hydratorNs, $this->config->getAutoGenerateHydratorClasses()); $this->unitOfWork = new UnitOfWork($this, $this->eventManager, $this->hydratorFactory); $this->hydratorFactory->setUnitOfWork($this->unitOfWork); $this->schemaManager = new SchemaManager($this, $this->metadataFactory); $this->proxyFactory = new ProxyFactory($this, $this->config->getProxyDir(), $this->config->getProxyNamespace(), $this->config->getAutoGenerateProxyClasses()); }
/** * INTERNAL: * Creates a document. Used for reconstitution of documents during hydration. * * @ignore * @param string $className The name of the document class. * @param array $data The data for the document. * @param array $hints Any hints to account for during reconstitution/lookup of the document. * @param object The document to be hydrated into in case of creation * @return object The document instance. * @internal Highly performance-sensitive method. */ public function getOrCreateDocument($className, $data, &$hints = array(), $document = null) { $class = $this->dm->getClassMetadata($className); // @TODO figure out how to remove this $discriminatorValue = null; if (isset($class->discriminatorField, $data[$class->discriminatorField])) { $discriminatorValue = $data[$class->discriminatorField]; } elseif (isset($class->defaultDiscriminatorValue)) { $discriminatorValue = $class->defaultDiscriminatorValue; } if ($discriminatorValue !== null) { $className = isset($class->discriminatorMap[$discriminatorValue]) ? $class->discriminatorMap[$discriminatorValue] : $discriminatorValue; $class = $this->dm->getClassMetadata($className); unset($data[$class->discriminatorField]); } $id = $class->getDatabaseIdentifierValue($data['_id']); $serializedId = serialize($id); if (isset($this->identityMap[$class->name][$serializedId])) { $document = $this->identityMap[$class->name][$serializedId]; $oid = spl_object_hash($document); if ($document instanceof Proxy && !$document->__isInitialized__) { $document->__isInitialized__ = true; $overrideLocalValues = true; if ($document instanceof NotifyPropertyChanged) { $document->addPropertyChangedListener($this); } } else { $overrideLocalValues = !empty($hints[Query::HINT_REFRESH]); } if ($overrideLocalValues) { $data = $this->hydratorFactory->hydrate($document, $data, $hints); $this->originalDocumentData[$oid] = $data; } } else { if ($document === null) { $document = $class->newInstance(); } $this->registerManaged($document, $id, $data); $oid = spl_object_hash($document); $this->documentStates[$oid] = self::STATE_MANAGED; $this->identityMap[$class->name][$serializedId] = $document; $data = $this->hydratorFactory->hydrate($document, $data, $hints); $this->originalDocumentData[$oid] = $data; } return $document; }
/** * INTERNAL: * Creates an document. Used for reconstitution of documents during hydration. * * @ignore * @param string $className The name of the document class. * @param array $data The data for the document. * @param array $hints Any hints to account for during reconstitution/lookup of the document. * @return object The document instance. * @internal Highly performance-sensitive method. */ public function getOrCreateDocument($className, $data, &$hints = array()) { $class = $this->dm->getClassMetadata($className); // @TODO figure out how to remove this if ($class->discriminatorField) { if (isset($data[$class->discriminatorField['name']])) { $type = $data[$class->discriminatorField['name']]; $class = $this->dm->getClassMetadata($class->discriminatorMap[$data[$class->discriminatorField['name']]]); unset($data[$class->discriminatorField['name']]); } } $id = $class->getPHPIdentifierValue($data['_id']); if (isset($this->identityMap[$class->rootDocumentName][$id])) { $document = $this->identityMap[$class->rootDocumentName][$id]; $oid = spl_object_hash($document); if ($document instanceof Proxy && !$document->__isInitialized__) { $document->__isInitialized__ = true; $overrideLocalValues = true; if ($document instanceof NotifyPropertyChanged) { $document->addPropertyChangedListener($this); } } else { $overrideLocalValues = isset($hints[Query::HINT_REFRESH]); } if ($overrideLocalValues) { $data = $this->hydratorFactory->hydrate($document, $data); $this->originalDocumentData[$oid] = $data; } } else { $document = $class->newInstance(); $this->registerManaged($document, $id, $data); $oid = spl_object_hash($document); $this->documentStates[$oid] = self::STATE_MANAGED; $this->identityMap[$class->rootDocumentName][$id] = $document; $data = $this->hydratorFactory->hydrate($document, $data); $this->originalDocumentData[$oid] = $data; } return $document; }