1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
4
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
5
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
6
|
|
|
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
7
|
|
|
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
8
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
9
|
|
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
10
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
11
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
12
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
13
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
14
|
|
|
* |
15
|
|
|
* This software consists of voluntary contributions made by many individuals |
16
|
|
|
* and is licensed under the MIT license. For more information, see |
17
|
|
|
* <http://www.doctrine-project.org>. |
18
|
|
|
*/ |
19
|
|
|
|
20
|
|
|
namespace Doctrine\ODM\MongoDB\Query; |
21
|
|
|
|
22
|
|
|
use Doctrine\ODM\MongoDB\DocumentManager; |
23
|
|
|
use Doctrine\ODM\MongoDB\Mapping\ClassMetadata; |
24
|
|
|
use Doctrine\ODM\MongoDB\Mapping\ClassMetadataInfo; |
25
|
|
|
use Doctrine\ODM\MongoDB\Mapping\MappingException; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Query expression builder for ODM. |
29
|
|
|
* |
30
|
|
|
* @since 1.0 |
31
|
|
|
*/ |
32
|
|
|
class Expr extends \Doctrine\MongoDB\Query\Expr |
33
|
|
|
{ |
34
|
|
|
/** |
35
|
|
|
* The DocumentManager instance for this query |
36
|
|
|
* |
37
|
|
|
* @var DocumentManager |
38
|
|
|
*/ |
39
|
|
|
private $dm; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* The ClassMetadata instance for the document being queried |
43
|
|
|
* |
44
|
|
|
* @var ClassMetadata |
45
|
|
|
*/ |
46
|
|
|
private $class; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @param DocumentManager $dm |
50
|
|
|
*/ |
51
|
244 |
|
public function __construct(DocumentManager $dm) |
52
|
|
|
{ |
53
|
244 |
|
$this->dm = $dm; |
54
|
244 |
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Sets ClassMetadata for document being queried. |
58
|
|
|
* |
59
|
|
|
* @param ClassMetadata $class |
60
|
|
|
*/ |
61
|
242 |
|
public function setClassMetadata(ClassMetadata $class) |
62
|
|
|
{ |
63
|
242 |
|
$this->class = $class; |
64
|
242 |
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Checks that the value of the current field is a reference to the supplied document. |
68
|
|
|
* |
69
|
|
|
* @param object $document |
70
|
|
|
* @return Expr |
71
|
|
|
*/ |
72
|
14 |
|
public function references($document) |
73
|
|
|
{ |
74
|
14 |
|
if ($this->currentField) { |
75
|
14 |
|
$mapping = $this->getReferenceMapping(); |
76
|
12 |
|
$reference = $this->dm->createReference($document, $mapping); |
77
|
12 |
|
$storeAs = array_key_exists('storeAs', $mapping) ? $mapping['storeAs'] : null; |
78
|
|
|
|
79
|
|
View Code Duplication |
switch ($storeAs) { |
|
|
|
|
80
|
12 |
|
case ClassMetadataInfo::REFERENCE_STORE_AS_ID: |
81
|
5 |
|
$this->query[$mapping['name']] = $reference; |
82
|
5 |
|
return $this; |
83
|
|
|
break; |
|
|
|
|
84
|
|
|
|
85
|
9 |
|
case ClassMetadataInfo::REFERENCE_STORE_AS_REF: |
86
|
|
|
$keys = ['id' => true]; |
87
|
|
|
break; |
88
|
|
|
|
89
|
9 |
|
case ClassMetadataInfo::REFERENCE_STORE_AS_DB_REF: |
90
|
7 |
|
case ClassMetadataInfo::REFERENCE_STORE_AS_DB_REF_WITH_DB: |
91
|
9 |
|
$keys = ['$ref' => true, '$id' => true, '$db' => true]; |
92
|
|
|
|
93
|
9 |
|
if ($storeAs === ClassMetadataInfo::REFERENCE_STORE_AS_DB_REF) { |
94
|
3 |
|
unset($keys['$db']); |
95
|
|
|
} |
96
|
|
|
|
97
|
9 |
|
if (isset($mapping['targetDocument'])) { |
98
|
5 |
|
unset($keys['$ref'], $keys['$db']); |
99
|
|
|
} |
100
|
9 |
|
break; |
101
|
|
|
|
102
|
|
|
default: |
103
|
|
|
throw new \InvalidArgumentException("Reference type {$storeAs} is invalid."); |
104
|
|
|
} |
105
|
|
|
|
106
|
9 |
View Code Duplication |
foreach ($keys as $key => $value) { |
|
|
|
|
107
|
9 |
|
$this->query[$mapping['name'] . '.' . $key] = $reference[$key]; |
108
|
|
|
} |
109
|
|
View Code Duplication |
} else { |
|
|
|
|
110
|
|
|
@trigger_error('Calling ' . __METHOD__ . ' without a current field set will no longer be possible in ODM 2.0.', E_USER_DEPRECATED); |
|
|
|
|
111
|
|
|
|
112
|
|
|
$this->query = $this->dm->createDBRef($document); |
|
|
|
|
113
|
|
|
} |
114
|
|
|
|
115
|
9 |
|
return $this; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Checks that the current field includes a reference to the supplied document. |
120
|
|
|
* |
121
|
|
|
* @param object $document |
122
|
|
|
* @return Expr |
123
|
|
|
*/ |
124
|
7 |
|
public function includesReferenceTo($document) |
125
|
|
|
{ |
126
|
7 |
|
if ($this->currentField) { |
127
|
7 |
|
$mapping = $this->getReferenceMapping(); |
128
|
5 |
|
$reference = $this->dm->createReference($document, $mapping); |
129
|
5 |
|
$storeAs = array_key_exists('storeAs', $mapping) ? $mapping['storeAs'] : null; |
130
|
|
|
|
131
|
|
View Code Duplication |
switch ($storeAs) { |
|
|
|
|
132
|
5 |
|
case ClassMetadataInfo::REFERENCE_STORE_AS_ID: |
133
|
3 |
|
$this->query[$mapping['name']] = $reference; |
134
|
3 |
|
return $this; |
135
|
|
|
break; |
|
|
|
|
136
|
|
|
|
137
|
4 |
|
case ClassMetadataInfo::REFERENCE_STORE_AS_REF: |
138
|
|
|
$keys = ['id' => true]; |
139
|
|
|
break; |
140
|
|
|
|
141
|
4 |
|
case ClassMetadataInfo::REFERENCE_STORE_AS_DB_REF: |
142
|
3 |
|
case ClassMetadataInfo::REFERENCE_STORE_AS_DB_REF_WITH_DB: |
143
|
4 |
|
$keys = ['$ref' => true, '$id' => true, '$db' => true]; |
144
|
|
|
|
145
|
4 |
|
if ($storeAs === ClassMetadataInfo::REFERENCE_STORE_AS_DB_REF) { |
146
|
2 |
|
unset($keys['$db']); |
147
|
|
|
} |
148
|
|
|
|
149
|
4 |
|
if (isset($mapping['targetDocument'])) { |
150
|
2 |
|
unset($keys['$ref'], $keys['$db']); |
151
|
|
|
} |
152
|
4 |
|
break; |
153
|
|
|
|
154
|
|
|
default: |
155
|
|
|
throw new \InvalidArgumentException("Reference type {$storeAs} is invalid."); |
156
|
|
|
} |
157
|
|
|
|
158
|
4 |
View Code Duplication |
foreach ($keys as $key => $value) { |
|
|
|
|
159
|
4 |
|
$this->query[$mapping['name']]['$elemMatch'][$key] = $reference[$key]; |
160
|
|
|
} |
161
|
|
View Code Duplication |
} else { |
|
|
|
|
162
|
|
|
@trigger_error('Calling ' . __METHOD__ . ' without a current field set will no longer be possible in ODM 2.0.', E_USER_DEPRECATED); |
|
|
|
|
163
|
|
|
|
164
|
|
|
$this->query['$elemMatch'] = $this->dm->createDBRef($document); |
|
|
|
|
165
|
|
|
} |
166
|
|
|
|
167
|
4 |
|
return $this; |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* Gets prepared query part of expression. |
172
|
|
|
* |
173
|
|
|
* @return array |
174
|
|
|
*/ |
175
|
230 |
|
public function getQuery() |
176
|
|
|
{ |
177
|
230 |
|
return $this->dm->getUnitOfWork() |
|
|
|
|
178
|
230 |
|
->getDocumentPersister($this->class->name) |
179
|
230 |
|
->prepareQueryOrNewObj($this->query); |
|
|
|
|
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* Gets prepared newObj part of expression. |
184
|
|
|
* |
185
|
|
|
* @return array |
186
|
|
|
*/ |
187
|
205 |
|
public function getNewObj() |
188
|
|
|
{ |
189
|
205 |
|
return $this->dm->getUnitOfWork() |
190
|
205 |
|
->getDocumentPersister($this->class->name) |
191
|
205 |
|
->prepareQueryOrNewObj($this->newObj, true); |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* Gets reference mapping for current field from current class or its descendants. |
196
|
|
|
* |
197
|
|
|
* @return array |
198
|
|
|
* @throws MappingException |
199
|
|
|
*/ |
200
|
21 |
|
private function getReferenceMapping() |
201
|
|
|
{ |
202
|
21 |
|
$mapping = null; |
203
|
|
|
try { |
204
|
21 |
|
$mapping = $this->class->getFieldMapping($this->currentField); |
205
|
6 |
|
} catch (MappingException $e) { |
206
|
6 |
|
if (empty($this->class->discriminatorMap)) { |
207
|
|
|
throw $e; |
208
|
|
|
} |
209
|
6 |
|
$foundIn = null; |
210
|
6 |
|
foreach ($this->class->discriminatorMap as $child) { |
211
|
6 |
|
$childClass = $this->dm->getClassMetadata($child); |
212
|
6 |
|
if ($childClass->hasAssociation($this->currentField)) { |
213
|
4 |
|
if ($mapping !== null && $mapping !== $childClass->getFieldMapping($this->currentField)) { |
214
|
2 |
|
throw MappingException::referenceFieldConflict($this->currentField, $foundIn->name, $childClass->name); |
215
|
|
|
} |
216
|
4 |
|
$mapping = $childClass->getFieldMapping($this->currentField); |
217
|
6 |
|
$foundIn = $childClass; |
218
|
|
|
} |
219
|
|
|
} |
220
|
4 |
|
if ($mapping === null) { |
221
|
2 |
|
throw MappingException::mappingNotFoundInClassNorDescendants($this->class->name, $this->currentField); |
222
|
|
|
} |
223
|
|
|
} |
224
|
17 |
|
return $mapping; |
225
|
|
|
} |
226
|
|
|
} |
227
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.