Skip to content

Commit

Permalink
Merge branch 'master' into odm-ng
Browse files Browse the repository at this point in the history
  • Loading branch information
malarzm committed Aug 5, 2016
2 parents bd16501 + 7bba4a2 commit 50605d6
Show file tree
Hide file tree
Showing 29 changed files with 227 additions and 97 deletions.
3 changes: 3 additions & 0 deletions docs/en/reference/annotations-reference.rst
Original file line number Diff line number Diff line change
Expand Up @@ -424,6 +424,9 @@ Optional attributes:
//...
}
.. note::
Requiring Indexes was deprecated in 1.2 and will be removed in 2.0.

@EmbedMany
----------

Expand Down
3 changes: 3 additions & 0 deletions docs/en/reference/indexes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -443,6 +443,9 @@ index.
Requiring Indexes
-----------------

.. note::
Requiring Indexes was deprecated in 1.2 and will be removed in 2.0.

Sometimes you may want to require indexes for all your queries to ensure you don't let stray unindexed queries
make it to the database and cause performance problems.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,16 @@ final class DiscriminatorField extends Annotation
{
/**
* Available for BC, but AnnotationDriver will consider $value first.
*
* @deprecated property was deprecated in 1.2 and will be removed in 2.0
*/
public $name;

/**
* Available for BC, but AnnotationDriver will consider $name and $value
* first.
*
* @deprecated
* @deprecated property was deprecated in 1.0.0-BETA10 and will be removed in 2.0
*/
public $fieldName;
}
1 change: 1 addition & 0 deletions lib/Doctrine/ODM/MongoDB/Mapping/Annotations/Document.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ final class Document extends AbstractDocument
public $collection;
public $repositoryClass;
public $indexes = array();
/** @deprecated */
public $requireIndexes = false;
public $shardKey;
public $slaveOkay;
Expand Down
4 changes: 4 additions & 0 deletions lib/Doctrine/ODM/MongoDB/Mapping/ClassMetadataInfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,8 @@ class ClassMetadataInfo implements \Doctrine\Common\Persistence\Mapping\ClassMet

/**
* READ-ONLY: Whether or not queries on this document should require indexes.
*
* @deprecated property was deprecated in 1.2 and will be removed in 2.0
*/
public $requireIndexes = false;

Expand Down Expand Up @@ -807,6 +809,8 @@ public function addIndex($keys, array $options = array())
* Set whether or not queries on this document should require indexes.
*
* @param bool $requireIndexes
*
* @deprecated method was deprecated in 1.2 and will be removed in 2.0
*/
public function setRequireIndexes($requireIndexes)
{
Expand Down
2 changes: 2 additions & 0 deletions lib/Doctrine/ODM/MongoDB/MongoDBException.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,8 @@ public static function cannotPersistMappedSuperclass($className)
* @param string $className
* @param string $unindexedFields
* @return MongoDBException
*
* @deprecated method was deprecated in 1.2 and will be removed in 2.0
*/
public static function queryNotIndexed($className, $unindexedFields)
{
Expand Down
100 changes: 68 additions & 32 deletions lib/Doctrine/ODM/MongoDB/Persisters/DocumentPersister.php
Original file line number Diff line number Diff line change
Expand Up @@ -908,9 +908,9 @@ public function prepareSortOrProjection(array $fields)
*/
public function prepareFieldName($fieldName)
{
list($fieldName) = $this->prepareQueryElement($fieldName, null, null, false);
$fieldNames = $this->prepareQueryElement($fieldName, null, null, false);

return $fieldName;
return $fieldNames[0][0];
}

/**
Expand Down Expand Up @@ -991,11 +991,12 @@ public function prepareQueryOrNewObj(array $query)
continue;
}

list($key, $value) = $this->prepareQueryElement($key, $value, null, true);

$preparedQuery[$key] = is_array($value)
? array_map('\Doctrine\ODM\MongoDB\Types\Type::convertPHPToDatabaseValue', $value)
: Type::convertPHPToDatabaseValue($value);
$preparedQueryElements = $this->prepareQueryElement($key, $value, null, true);
foreach ($preparedQueryElements as list($preparedKey, $preparedValue)) {
$preparedQuery[$preparedKey] = is_array($preparedValue)
? array_map('\Doctrine\ODM\MongoDB\Types\Type::convertPHPToDatabaseValue', $preparedValue)
: Type::convertPHPToDatabaseValue($preparedValue);
}
}

return $preparedQuery;
Expand All @@ -1011,7 +1012,7 @@ public function prepareQueryOrNewObj(array $query)
* @param mixed $value
* @param ClassMetadata $class Defaults to $this->class
* @param boolean $prepareValue Whether or not to prepare the value
* @return array Prepared field name and value
* @return array An array of tuples containing prepared field names and values
*/
private function prepareQueryElement($fieldName, $value = null, $class = null, $prepareValue = true)
{
Expand All @@ -1025,18 +1026,18 @@ private function prepareQueryElement($fieldName, $value = null, $class = null, $
$fieldName = $mapping['name'];

if ( ! $prepareValue) {
return array($fieldName, $value);
return [[$fieldName, $value]];
}

// Prepare mapped, embedded objects
if ( ! empty($mapping['embedded']) && is_object($value) &&
! $this->dm->getMetadataFactory()->isTransient(get_class($value))) {
return array($fieldName, $this->pb->prepareEmbeddedDocumentValue($mapping, $value));
return [[$fieldName, $this->pb->prepareEmbeddedDocumentValue($mapping, $value)]];
}

if (! empty($mapping['reference']) && is_object($value) && ! ($value instanceof \MongoId)) {
try {
return array($fieldName, $this->dm->createDBRef($value, $mapping));
return $this->prepareDbRefElement($fieldName, $value, $mapping);
} catch (MappingException $e) {
// do nothing in case passed object is not mapped document
}
Expand All @@ -1046,47 +1047,47 @@ private function prepareQueryElement($fieldName, $value = null, $class = null, $
// We can't have expressions in empty() with PHP < 5.5, so store it in a variable
$arrayValue = (array) $value;
if (empty($mapping['reference']) || $mapping['storeAs'] !== ClassMetadataInfo::REFERENCE_STORE_AS_ID || empty($arrayValue)) {
return array($fieldName, $value);
return [[$fieldName, $value]];
}

// Additional preparation for one or more simple reference values
$targetClass = $this->dm->getClassMetadata($mapping['targetDocument']);

if ( ! is_array($value)) {
return array($fieldName, $targetClass->getDatabaseIdentifierValue($value));
return [[$fieldName, $targetClass->getDatabaseIdentifierValue($value)]];
}

// Objects without operators or with DBRef fields can be converted immediately
if ( ! $this->hasQueryOperators($value) || $this->hasDBRefFields($value)) {
return array($fieldName, $targetClass->getDatabaseIdentifierValue($value));
return [[$fieldName, $targetClass->getDatabaseIdentifierValue($value)]];
}

return array($fieldName, $this->prepareQueryExpression($value, $targetClass));
return [[$fieldName, $this->prepareQueryExpression($value, $targetClass)]];
}

// Process identifier fields
if (($class->hasField($fieldName) && $class->isIdentifier($fieldName)) || $fieldName === '_id') {
$fieldName = '_id';

if ( ! $prepareValue) {
return array($fieldName, $value);
return [[$fieldName, $value]];
}

if ( ! is_array($value)) {
return array($fieldName, $class->getDatabaseIdentifierValue($value));
return [[$fieldName, $class->getDatabaseIdentifierValue($value)]];
}

// Objects without operators or with DBRef fields can be converted immediately
if ( ! $this->hasQueryOperators($value) || $this->hasDBRefFields($value)) {
return array($fieldName, $class->getDatabaseIdentifierValue($value));
return [[$fieldName, $class->getDatabaseIdentifierValue($value)]];
}

return array($fieldName, $this->prepareQueryExpression($value, $class));
return [[$fieldName, $this->prepareQueryExpression($value, $class)]];
}

// No processing for unmapped, non-identifier, non-dotted field names
if (strpos($fieldName, '.') === false) {
return array($fieldName, $value);
return [[$fieldName, $value]];
}

/* Process "fieldName.objectProperty" queries (on arrays or objects).
Expand All @@ -1099,7 +1100,7 @@ private function prepareQueryElement($fieldName, $value = null, $class = null, $

// No further processing for unmapped fields
if ( ! isset($class->fieldMappings[$e[0]])) {
return array($fieldName, $value);
return [[$fieldName, $value]];
}

$mapping = $class->fieldMappings[$e[0]];
Expand All @@ -1109,7 +1110,7 @@ private function prepareQueryElement($fieldName, $value = null, $class = null, $
if ($mapping['type'] === Type::HASH || $mapping['type'] === Type::RAW) {
$fieldName = implode('.', $e);

return array($fieldName, $value);
return [[$fieldName, $value]];
}

if ($mapping['type'] == 'many' && CollectionHelper::isHash($mapping['strategy'])
Expand All @@ -1130,7 +1131,7 @@ private function prepareQueryElement($fieldName, $value = null, $class = null, $
} else {
$fieldName = $e[0] . '.' . $e[1];

return array($fieldName, $value);
return [[$fieldName, $value]];
}

// No further processing for fields without a targetDocument mapping
Expand All @@ -1139,7 +1140,7 @@ private function prepareQueryElement($fieldName, $value = null, $class = null, $
$fieldName .= '.'.$nextObjectProperty;
}

return array($fieldName, $value);
return [[$fieldName, $value]];
}

$targetClass = $this->dm->getClassMetadata($mapping['targetDocument']);
Expand All @@ -1150,7 +1151,7 @@ private function prepareQueryElement($fieldName, $value = null, $class = null, $
$fieldName .= '.'.$nextObjectProperty;
}

return array($fieldName, $value);
return [[$fieldName, $value]];
}

$targetMapping = $targetClass->getFieldMapping($objectProperty);
Expand All @@ -1164,19 +1165,19 @@ private function prepareQueryElement($fieldName, $value = null, $class = null, $
// Process targetDocument identifier fields
if ($objectPropertyIsId) {
if ( ! $prepareValue) {
return array($fieldName, $value);
return [[$fieldName, $value]];
}

if ( ! is_array($value)) {
return array($fieldName, $targetClass->getDatabaseIdentifierValue($value));
return [[$fieldName, $targetClass->getDatabaseIdentifierValue($value)]];
}

// Objects without operators or with DBRef fields can be converted immediately
if ( ! $this->hasQueryOperators($value) || $this->hasDBRefFields($value)) {
return array($fieldName, $targetClass->getDatabaseIdentifierValue($value));
return [[$fieldName, $targetClass->getDatabaseIdentifierValue($value)]];
}

return array($fieldName, $this->prepareQueryExpression($value, $targetClass));
return [[$fieldName, $this->prepareQueryExpression($value, $targetClass)]];
}

/* The property path may include a third field segment, excluding the
Expand All @@ -1189,12 +1190,16 @@ private function prepareQueryElement($fieldName, $value = null, $class = null, $
? $this->dm->getClassMetadata($targetMapping['targetDocument'])
: null;

list($key, $value) = $this->prepareQueryElement($nextObjectProperty, $value, $nextTargetClass, $prepareValue);
$fieldNames = $this->prepareQueryElement($nextObjectProperty, $value, $nextTargetClass, $prepareValue);

return array_map(function ($preparedTuple) use ($fieldName) {
list($key, $value) = $preparedTuple;

$fieldName .= '.' . $key;
return [$fieldName . '.' . $key, $value];
}, $fieldNames);
}

return array($fieldName, $value);
return [[$fieldName, $value]];
}

/**
Expand Down Expand Up @@ -1390,4 +1395,35 @@ private function getWriteOptions(array $options = array())

return array_merge($defaultOptions, $documentOptions, $options);
}

/**
* @param string $fieldName
* @param mixed $value
* @param array $mapping
* @return array
*/
private function prepareDbRefElement($fieldName, $value, array $mapping)
{
$dbRef = $this->dm->createDBRef($value, $mapping);
$keys = ['$ref' => true, '$id' => true, '$db' => true];
if ($mapping['storeAs'] === ClassMetadataInfo::REFERENCE_STORE_AS_ID) {
unset($keys['$db']);
}
if (isset($mapping['targetDocument'])) {
unset($keys['$ref'], $keys['$db']);
}

if ($mapping['storeAs'] === ClassMetadataInfo::REFERENCE_STORE_AS_ID) {
return [[$fieldName, $dbRef]];
} elseif ($mapping['type'] === 'many') {
return [[$fieldName, ['$elemMatch' => array_intersect_key($dbRef, $keys)]]];
} else {
return array_map(
function ($key) use ($dbRef, $fieldName) {
return [$fieldName . '.' . $key, $dbRef[$key]];
},
array_keys($keys)
);
}
}
}
2 changes: 2 additions & 0 deletions lib/Doctrine/ODM/MongoDB/Query/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,8 @@ public function __construct(DocumentManager $dm, $documentName = null)
*
* @param bool $requireIndexes
* @return $this
*
* @deprecated method was deprecated in 1.2 and will be removed in 2.0
*/
public function requireIndexes($requireIndexes = true)
{
Expand Down
2 changes: 2 additions & 0 deletions lib/Doctrine/ODM/MongoDB/Query/FieldExtractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
* a given mongodb query. Used for checking if query is indexed.
*
* @see Doctrine\ODM\MongoDB\Query::isIndexed()
*
* @deprecated class was deprecated in 1.2 and will be removed in 2.0
*/
class FieldExtractor
{
Expand Down
10 changes: 9 additions & 1 deletion lib/Doctrine/ODM/MongoDB/Query/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ class Query extends \Doctrine\MongoDB\Query\Query
/**
* Constructor.
*
* Please note that $requireIndexes was deprecated in 1.2 and will be removed in 2.0
*
* @param DocumentManager $dm
* @param ClassMetadata $class
* @param Collection $collection
Expand All @@ -95,7 +97,7 @@ class Query extends \Doctrine\MongoDB\Query\Query
* @param boolean $hydrate
* @param boolean $refresh
* @param array $primers
* @param null $requireIndexes
* @param null $requireIndexes deprecated
* @param boolean $readOnly
*/
public function __construct(DocumentManager $dm, ClassMetadata $class, Collection $collection, array $query = array(), array $options = array(), $hydrate = true, $refresh = false, array $primers = array(), $requireIndexes = null, $readOnly = false)
Expand Down Expand Up @@ -190,6 +192,8 @@ public function setRefresh($refresh)
* Gets the fields involved in this query.
*
* @return array $fields An array of fields names used in this query.
*
* @deprecated method was deprecated in 1.2 and will be removed in 2.0
*/
public function getFieldsInQuery()
{
Expand All @@ -204,6 +208,8 @@ public function getFieldsInQuery()
* Check if this query is indexed.
*
* @return bool
*
* @deprecated method was deprecated in 1.2 and will be removed in 2.0
*/
public function isIndexed()
{
Expand All @@ -220,6 +226,8 @@ public function isIndexed()
* Gets an array of the unindexed fields in this query.
*
* @return array
*
* @deprecated method was deprecated in 1.2 and will be removed in 2.0
*/
public function getUnindexedFields()
{
Expand Down
Loading

0 comments on commit 50605d6

Please sign in to comment.