/** * Prepares a query value and converts the php value to the database value if it is an identifier. * It also handles converting $fieldName to the database name if they are different. * * @param string $fieldName * @param string $value * @return mixed $value */ private function prepareQueryValue(&$fieldName, $value) { // Process "association.fieldName" if (strpos($fieldName, '.') !== false) { $e = explode('.', $fieldName); $mapping = $this->class->getFieldMapping($e[0]); if ($this->class->hasField($e[0])) { $name = $this->class->fieldMappings[$e[0]]['name']; if ($name !== $e[0]) { $e[0] = $name; } } if (isset($mapping['targetDocument'])) { $targetClass = $this->dm->getClassMetadata($mapping['targetDocument']); if ($targetClass->hasField($e[1])) { if ($targetClass->identifier === $e[1] || $e[1] === '$id') { $fieldName = $e[0] . '.$id'; $value = $targetClass->getDatabaseIdentifierValue($value); } } } // Process all non identifier fields } elseif ($this->class->hasField($fieldName) && !$this->class->isIdentifier($fieldName)) { $name = $this->class->fieldMappings[$fieldName]['name']; $mapping = $this->class->fieldMappings[$fieldName]; if ($name !== $fieldName) { $fieldName = $name; } // Process identifier } elseif ($fieldName === $this->class->identifier || $fieldName === '_id') { $fieldName = '_id'; $value = $this->class->getDatabaseIdentifierValue($value); } return $value; }
/** * Prepares a query value and converts the php value to the database value if it is an identifier. * It also handles converting $fieldName to the database name if they are different. * * @param string $fieldName * @param string $value * @return mixed $value */ private function prepareQueryValue(&$fieldName, $value) { // Process "association.fieldName" if (strpos($fieldName, '.') !== false) { $e = explode('.', $fieldName); $mapping = $this->class->getFieldMapping($e[0]); $name = $mapping['name']; if ($name !== $e[0]) { $e[0] = $name; } if (isset($mapping['targetDocument'])) { $targetClass = $this->dm->getClassMetadata($mapping['targetDocument']); if ($targetClass->hasField($e[1])) { if ($targetClass->identifier === $e[1]) { $e[1] = '$id'; if (is_array($value)) { foreach ($value as $k => $v) { $value[$k] = $targetClass->getDatabaseIdentifierValue($v); } } else { $value = $targetClass->getDatabaseIdentifierValue($value); } } else { $targetMapping = $targetClass->getFieldMapping($e[1]); $targetName = $targetMapping['name']; if ($targetName !== $e[1]) { $e[1] = $targetName; } } $fieldName = $e[0] . '.' . $e[1]; } } // Process all non identifier fields // We only change the field names here to the mongodb field name used for persistence } elseif ($this->class->hasField($fieldName) && !$this->class->isIdentifier($fieldName)) { $name = $this->class->fieldMappings[$fieldName]['name']; $mapping = $this->class->fieldMappings[$fieldName]; if ($name !== $fieldName) { $fieldName = $name; } // Process identifier } elseif ($this->class->hasField($fieldName) && $this->class->isIdentifier($fieldName) || $fieldName === '_id') { $fieldName = '_id'; if (is_array($value)) { foreach ($value as $k => $v) { if ($k[0] === '$' && is_array($v)) { foreach ($v as $k2 => $v2) { $value[$k][$k2] = $this->class->getDatabaseIdentifierValue($v2); } } else { $value[$k] = $this->class->getDatabaseIdentifierValue($v); } } } else { $value = $this->class->getDatabaseIdentifierValue($value); } } return $value; }
/** * Releases any lock that exists on this document. * * @param object $document */ public function unlock($document) { $id = $this->uow->getDocumentIdentifier($document); $criteria = array('_id' => $this->class->getDatabaseIdentifierValue($id)); $lockMapping = $this->class->fieldMappings[$this->class->lockField]; $this->collection->update($criteria, array($this->cmd . 'unset' => array($lockMapping['name'] => true))); $this->class->reflFields[$this->class->lockField]->setValue($document, null); }
/** * Lood document by its identifier. * * @param string $id * @return object|null */ public function loadById($id) { $result = $this->collection->findOne(array('_id' => $this->class->getDatabaseIdentifierValue($id))); if ($result !== null) { return $this->uow->getOrCreateDocument($this->documentName, $result); } return null; }
/** * Get shard key aware query for single document. * * @param object $document * * @return array */ private function getQueryForDocument($document) { $id = $this->uow->getDocumentIdentifier($document); $id = $this->class->getDatabaseIdentifierValue($id); $shardKeyQueryPart = $this->getShardKeyQuery($document); $query = array_merge(array('_id' => $id), $shardKeyQueryPart); return $query; }
/** * Lood document by its identifier. * * @param string $id * @return object|null */ public function loadById($id) { $result = $this->_collection->findOne(array('_id' => $this->_class->getDatabaseIdentifierValue($id))); if ($result !== null) { $document = $this->_uow->getOrCreateDocument($this->_documentName, $result); $this->_uow->registerManaged($document, $this->_class->getPHPIdentifierValue($result['_id']), $result); return $document; } return null; }
/** * Prepare where values converting document object field names to the document collection * field name. * * @param string $fieldName * @param string $value * @return string $value */ private function prepareWhereValue(&$fieldName, $value) { if (strpos($fieldName, '.') !== false) { $e = explode('.', $fieldName); $mapping = $this->class->getFieldMapping($e[0]); if ($this->class->hasField($e[0])) { $name = $this->class->fieldMappings[$e[0]]['name']; if ($name !== $e[0]) { $e[0] = $name; } } if (isset($mapping['targetDocument'])) { $targetClass = $this->dm->getClassMetadata($mapping['targetDocument']); if ($targetClass->hasField($e[1]) && $targetClass->identifier === $e[1]) { $fieldName = $e[0] . '.$id'; $value = $targetClass->getDatabaseIdentifierValue($value); } elseif ($e[1] === '$id') { $value = $targetClass->getDatabaseIdentifierValue($value); } } } elseif ($this->class->hasField($fieldName) && ! $this->class->isIdentifier($fieldName)) { $name = $this->class->fieldMappings[$fieldName]['name']; if ($name !== $fieldName) { $fieldName = $name; } } else { if ($fieldName === $this->class->identifier || $fieldName === '_id') { $fieldName = '_id'; if (is_array($value)) { if (isset($value[$this->cmd.'in'])) { foreach ($value[$this->cmd.'in'] as $k => $v) { $value[$this->cmd.'in'][$k] = $this->class->getDatabaseIdentifierValue($v); } } else { foreach ($value as $k => $v) { $value[$k] = $this->class->getDatabaseIdentifierValue($v); } } } else { $value = $this->class->getDatabaseIdentifierValue($value); } } } return $value; }
/** * INTERNAL: * Tries to get a document by its identifier hash. If no document is found * for the given hash, FALSE is returned. * * @ignore * @param mixed $id Document identifier * @param ClassMetadata $class Document class * @return mixed The found document or FALSE. * @throws InvalidArgumentException if the class does not have an identifier */ public function tryGetById($id, ClassMetadata $class) { if (!$class->identifier) { throw new \InvalidArgumentException(sprintf('Class "%s" does not have an identifier', $class->name)); } $serializedId = serialize($class->getDatabaseIdentifierValue($id)); return isset($this->identityMap[$class->name][$serializedId]) ? $this->identityMap[$class->name][$serializedId] : false; }
/** * Prepares a query expression. * * @param array|object $expression * @param ClassMetadata $class * @return array */ private function prepareQueryExpression($expression, $class) { foreach ($expression as $k => $v) { // Ignore query operators whose arguments need no type conversion if (in_array($k, array('$exists', '$type', '$mod', '$size'))) { continue; } // Process query operators whose argument arrays need type conversion if (in_array($k, array('$in', '$nin', '$all')) && is_array($v)) { foreach ($v as $k2 => $v2) { $expression[$k][$k2] = $class->getDatabaseIdentifierValue($v2); } continue; } // Recursively process expressions within a $not operator if ($k === '$not' && is_array($v)) { $expression[$k] = $this->prepareQueryExpression($v, $class); continue; } $expression[$k] = $class->getDatabaseIdentifierValue($v); } return $expression; }
/** * Gets the document database identifier value for the given document. * * @param object $document * @param ClassMetadata $class * @return mixed $id */ private function getDocumentId($document, ClassMetadata $class) { return $class->getDatabaseIdentifierValue($this->uow->getDocumentIdentifier($document)); }
/** * INTERNAL: * Tries to get a document by its identifier hash. If no document is found * for the given hash, FALSE is returned. * * @ignore * @param mixed $id Document identifier * @param ClassMetadata $class Document class * @return mixed The found document or FALSE. * @throws InvalidArgumentException if the class does not have an identifier */ public function tryGetById($id, ClassMetadata $class) { if (!$class->identifier) { throw new \InvalidArgumentException(sprintf('Class "%s" does not have an identifier', $class->name)); } $serializedId = serialize($class->getDatabaseIdentifierValue($id)); /* Trying all the classes from the discriminator map as well * to avoid issuing extra DB query in case of using simple reference. */ foreach ($class->discriminatorMap as $className) { // Never return parent instances, only children if (in_array($className, $class->parentClasses)) { continue; } if (isset($this->identityMap[$className][$serializedId])) { return $this->identityMap[$className][$serializedId]; } } return isset($this->identityMap[$class->name][$serializedId]) ? $this->identityMap[$class->name][$serializedId] : false; }