1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Doctrine\Tests\ORM\Functional; |
4
|
|
|
|
5
|
|
|
use Doctrine\Common\Collections\ArrayCollection; |
6
|
|
|
|
7
|
|
|
use Doctrine\ORM\NonUniqueResultException; |
8
|
|
|
use Doctrine\ORM\Proxy\Proxy; |
9
|
|
|
use Doctrine\ORM\Query\QueryException; |
10
|
|
|
use Doctrine\ORM\UnexpectedResultException; |
11
|
|
|
use Doctrine\Tests\Models\CMS\CmsUser, |
12
|
|
|
Doctrine\Tests\Models\CMS\CmsArticle, |
13
|
|
|
Doctrine\Tests\Models\CMS\CmsPhonenumber; |
14
|
|
|
use Doctrine\ORM\Mapping\ClassMetadata; |
15
|
|
|
use Doctrine\ORM\Query; |
16
|
|
|
use Doctrine\ORM\Query\Parameter; |
17
|
|
|
use Doctrine\Tests\OrmFunctionalTestCase; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Functional Query tests. |
21
|
|
|
* |
22
|
|
|
* @author robo |
23
|
|
|
*/ |
24
|
|
|
class QueryTest extends OrmFunctionalTestCase |
25
|
|
|
{ |
26
|
|
|
protected function setUp() |
27
|
|
|
{ |
28
|
|
|
$this->useModelSet('cms'); |
29
|
|
|
|
30
|
|
|
parent::setUp(); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
public function testSimpleQueries() |
34
|
|
|
{ |
35
|
|
|
$user = new CmsUser; |
36
|
|
|
$user->name = 'Guilherme'; |
37
|
|
|
$user->username = 'gblanco'; |
38
|
|
|
$user->status = 'developer'; |
39
|
|
|
$this->_em->persist($user); |
40
|
|
|
$this->_em->flush(); |
41
|
|
|
$this->_em->clear(); |
42
|
|
|
|
43
|
|
|
$query = $this->_em->createQuery("select u, upper(u.name) from Doctrine\Tests\Models\CMS\CmsUser u where u.username = 'gblanco'"); |
44
|
|
|
|
45
|
|
|
$result = $query->getResult(); |
46
|
|
|
|
47
|
|
|
$this->assertEquals(1, count($result)); |
48
|
|
|
$this->assertInstanceOf(CmsUser::class, $result[0][0]); |
49
|
|
|
$this->assertEquals('Guilherme', $result[0][0]->name); |
50
|
|
|
$this->assertEquals('gblanco', $result[0][0]->username); |
51
|
|
|
$this->assertEquals('developer', $result[0][0]->status); |
52
|
|
|
$this->assertEquals('GUILHERME', $result[0][1]); |
53
|
|
|
|
54
|
|
|
$resultArray = $query->getArrayResult(); |
55
|
|
|
$this->assertEquals(1, count($resultArray)); |
56
|
|
|
$this->assertTrue(is_array($resultArray[0][0])); |
57
|
|
|
$this->assertEquals('Guilherme', $resultArray[0][0]['name']); |
58
|
|
|
$this->assertEquals('gblanco', $resultArray[0][0]['username']); |
59
|
|
|
$this->assertEquals('developer', $resultArray[0][0]['status']); |
60
|
|
|
$this->assertEquals('GUILHERME', $resultArray[0][1]); |
61
|
|
|
|
62
|
|
|
$scalarResult = $query->getScalarResult(); |
63
|
|
|
$this->assertEquals(1, count($scalarResult)); |
64
|
|
|
$this->assertEquals('Guilherme', $scalarResult[0]['u_name']); |
65
|
|
|
$this->assertEquals('gblanco', $scalarResult[0]['u_username']); |
66
|
|
|
$this->assertEquals('developer', $scalarResult[0]['u_status']); |
67
|
|
|
$this->assertEquals('GUILHERME', $scalarResult[0][1]); |
68
|
|
|
|
69
|
|
|
$query = $this->_em->createQuery("select upper(u.name) from Doctrine\Tests\Models\CMS\CmsUser u where u.username = 'gblanco'"); |
70
|
|
|
$this->assertEquals('GUILHERME', $query->getSingleScalarResult()); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
public function testJoinQueries() |
74
|
|
|
{ |
75
|
|
|
$user = new CmsUser; |
76
|
|
|
$user->name = 'Guilherme'; |
77
|
|
|
$user->username = 'gblanco'; |
78
|
|
|
$user->status = 'developer'; |
79
|
|
|
|
80
|
|
|
$article1 = new CmsArticle; |
81
|
|
|
$article1->topic = "Doctrine 2"; |
82
|
|
|
$article1->text = "This is an introduction to Doctrine 2."; |
83
|
|
|
$user->addArticle($article1); |
84
|
|
|
|
85
|
|
|
$article2 = new CmsArticle; |
86
|
|
|
$article2->topic = "Symfony 2"; |
87
|
|
|
$article2->text = "This is an introduction to Symfony 2."; |
88
|
|
|
$user->addArticle($article2); |
89
|
|
|
|
90
|
|
|
$this->_em->persist($user); |
91
|
|
|
$this->_em->persist($article1); |
92
|
|
|
$this->_em->persist($article2); |
93
|
|
|
|
94
|
|
|
$this->_em->flush(); |
95
|
|
|
$this->_em->clear(); |
96
|
|
|
|
97
|
|
|
$query = $this->_em->createQuery('select u, a from ' . CmsUser::class . ' u join u.articles a ORDER BY a.topic'); |
98
|
|
|
$users = $query->getResult(); |
99
|
|
|
$this->assertEquals(1, count($users)); |
100
|
|
|
$this->assertInstanceOf(CmsUser::class, $users[0]); |
101
|
|
|
$this->assertEquals(2, count($users[0]->articles)); |
102
|
|
|
$this->assertEquals('Doctrine 2', $users[0]->articles[0]->topic); |
103
|
|
|
$this->assertEquals('Symfony 2', $users[0]->articles[1]->topic); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
public function testUsingZeroBasedQueryParameterShouldWork() |
107
|
|
|
{ |
108
|
|
|
$user = new CmsUser; |
109
|
|
|
$user->name = 'Jonathan'; |
110
|
|
|
$user->username = 'jwage'; |
111
|
|
|
$user->status = 'developer'; |
112
|
|
|
$this->_em->persist($user); |
113
|
|
|
$this->_em->flush(); |
114
|
|
|
$this->_em->clear(); |
115
|
|
|
|
116
|
|
|
$q = $this->_em->createQuery('SELECT u FROM ' . CmsUser::class . ' u WHERE u.username = ?0'); |
117
|
|
|
$q->setParameter(0, 'jwage'); |
118
|
|
|
$user = $q->getSingleResult(); |
119
|
|
|
|
120
|
|
|
$this->assertNotNull($user); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
View Code Duplication |
public function testUsingUnknownQueryParameterShouldThrowException() |
124
|
|
|
{ |
125
|
|
|
$this->expectException(QueryException::class); |
126
|
|
|
$this->expectExceptionMessage('Invalid parameter: token 2 is not defined in the query.'); |
127
|
|
|
|
128
|
|
|
$q = $this->_em->createQuery('SELECT u FROM ' . CmsUser::class . ' u WHERE u.name = ?1'); |
129
|
|
|
$q->setParameter(2, 'jwage'); |
130
|
|
|
$user = $q->getSingleResult(); |
|
|
|
|
131
|
|
|
} |
132
|
|
|
|
133
|
|
View Code Duplication |
public function testTooManyParametersShouldThrowException() |
134
|
|
|
{ |
135
|
|
|
$this->expectException(QueryException::class); |
136
|
|
|
$this->expectExceptionMessage('Too many parameters: the query defines 1 parameters and you bound 2'); |
137
|
|
|
|
138
|
|
|
$q = $this->_em->createQuery('SELECT u FROM ' . CmsUser::class . ' u WHERE u.name = ?1'); |
139
|
|
|
$q->setParameter(1, 'jwage'); |
140
|
|
|
$q->setParameter(2, 'jwage'); |
141
|
|
|
|
142
|
|
|
$user = $q->getSingleResult(); |
|
|
|
|
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
public function testTooFewParametersShouldThrowException() |
146
|
|
|
{ |
147
|
|
|
$this->expectException(QueryException::class); |
148
|
|
|
$this->expectExceptionMessage('Too few parameters: the query defines 1 parameters but you only bound 0'); |
149
|
|
|
|
150
|
|
|
$this->_em->createQuery('SELECT u FROM ' . CmsUser::class . ' u WHERE u.name = ?1') |
151
|
|
|
->getSingleResult(); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
public function testInvalidInputParameterThrowsException() |
155
|
|
|
{ |
156
|
|
|
$this->expectException(QueryException::class); |
157
|
|
|
|
158
|
|
|
$this->_em->createQuery('SELECT u FROM ' . CmsUser::class . ' u WHERE u.name = ?') |
159
|
|
|
->setParameter(1, 'jwage') |
160
|
|
|
->getSingleResult(); |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
public function testSetParameters() |
164
|
|
|
{ |
165
|
|
|
$parameters = new ArrayCollection(); |
166
|
|
|
$parameters->add(new Parameter(1, 'jwage')); |
167
|
|
|
$parameters->add(new Parameter(2, 'active')); |
168
|
|
|
|
169
|
|
|
$this->_em->createQuery('SELECT u FROM ' . CmsUser::class . ' u WHERE u.name = ?1 AND u.status = ?2') |
170
|
|
|
->setParameters($parameters) |
171
|
|
|
->getResult(); |
172
|
|
|
|
173
|
|
|
$extractValue = function (Parameter $parameter) { |
174
|
|
|
return $parameter->getValue(); |
175
|
|
|
}; |
176
|
|
|
|
177
|
|
|
self::assertSame( |
178
|
|
|
$parameters->map($extractValue)->toArray(), |
179
|
|
|
$this->_sqlLoggerStack->queries[$this->_sqlLoggerStack->currentQuery]['params'] |
180
|
|
|
); |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
public function testSetParametersBackwardsCompatible() |
184
|
|
|
{ |
185
|
|
|
$parameters = [1 => 'jwage', 2 => 'active']; |
186
|
|
|
|
187
|
|
|
$this->_em->createQuery('SELECT u FROM ' . CmsUser::class . ' u WHERE u.name = ?1 AND u.status = ?2') |
188
|
|
|
->setParameters($parameters) |
189
|
|
|
->getResult(); |
190
|
|
|
|
191
|
|
|
self::assertSame( |
192
|
|
|
array_values($parameters), |
193
|
|
|
$this->_sqlLoggerStack->queries[$this->_sqlLoggerStack->currentQuery]['params'] |
194
|
|
|
); |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
/** |
198
|
|
|
* @group DDC-1070 |
199
|
|
|
*/ |
200
|
|
|
public function testIterateResultAsArrayAndParams() |
201
|
|
|
{ |
202
|
|
|
$article1 = new CmsArticle; |
203
|
|
|
$article1->topic = "Doctrine 2"; |
204
|
|
|
$article1->text = "This is an introduction to Doctrine 2."; |
205
|
|
|
|
206
|
|
|
$article2 = new CmsArticle; |
207
|
|
|
$article2->topic = "Symfony 2"; |
208
|
|
|
$article2->text = "This is an introduction to Symfony 2."; |
209
|
|
|
|
210
|
|
|
$this->_em->persist($article1); |
211
|
|
|
$this->_em->persist($article2); |
212
|
|
|
|
213
|
|
|
$this->_em->flush(); |
214
|
|
|
$this->_em->clear(); |
215
|
|
|
$articleId = $article1->id; |
216
|
|
|
|
217
|
|
|
$query = $this->_em->createQuery('select a from ' . CmsArticle::class . ' a WHERE a.topic = ?1'); |
218
|
|
|
$articles = $query->iterate(new ArrayCollection([new Parameter(1, 'Doctrine 2')]), Query::HYDRATE_ARRAY); |
219
|
|
|
|
220
|
|
|
$found = []; |
221
|
|
|
|
222
|
|
|
foreach ($articles AS $article) { |
223
|
|
|
$found[] = $article; |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
$this->assertEquals(1, count($found)); |
227
|
|
|
$this->assertSame( |
228
|
|
|
[ |
229
|
|
|
[ |
230
|
|
|
[ |
231
|
|
|
'id' => $articleId, |
232
|
|
|
'topic' => 'Doctrine 2', |
233
|
|
|
'text' => 'This is an introduction to Doctrine 2.', |
234
|
|
|
'version' => 1, |
235
|
|
|
], |
236
|
|
|
], |
237
|
|
|
], |
238
|
|
|
$found |
239
|
|
|
); |
240
|
|
|
} |
241
|
|
|
|
242
|
|
|
public function testIterateResult_IterativelyBuildUpUnitOfWork() |
243
|
|
|
{ |
244
|
|
|
$article1 = new CmsArticle; |
245
|
|
|
$article1->topic = "Doctrine 2"; |
246
|
|
|
$article1->text = "This is an introduction to Doctrine 2."; |
247
|
|
|
|
248
|
|
|
$article2 = new CmsArticle; |
249
|
|
|
$article2->topic = "Symfony 2"; |
250
|
|
|
$article2->text = "This is an introduction to Symfony 2."; |
251
|
|
|
|
252
|
|
|
$this->_em->persist($article1); |
253
|
|
|
$this->_em->persist($article2); |
254
|
|
|
|
255
|
|
|
$this->_em->flush(); |
256
|
|
|
$this->_em->clear(); |
257
|
|
|
|
258
|
|
|
$query = $this->_em->createQuery('select a from ' . CmsArticle::class . ' a'); |
259
|
|
|
$articles = $query->iterate(); |
260
|
|
|
|
261
|
|
|
$iteratedCount = 0; |
262
|
|
|
$topics = []; |
263
|
|
|
|
264
|
|
|
foreach($articles AS $row) { |
265
|
|
|
$article = $row[0]; |
266
|
|
|
$topics[] = $article->topic; |
267
|
|
|
|
268
|
|
|
$identityMap = $this->_em->getUnitOfWork()->getIdentityMap(); |
269
|
|
|
$identityMapCount = count($identityMap[CmsArticle::class]); |
270
|
|
|
$this->assertTrue($identityMapCount>$iteratedCount); |
271
|
|
|
|
272
|
|
|
$iteratedCount++; |
273
|
|
|
} |
274
|
|
|
|
275
|
|
|
$this->assertSame(["Doctrine 2", "Symfony 2"], $topics); |
276
|
|
|
$this->assertSame(2, $iteratedCount); |
277
|
|
|
|
278
|
|
|
$this->_em->flush(); |
279
|
|
|
$this->_em->clear(); |
280
|
|
|
} |
281
|
|
|
|
282
|
|
|
public function testIterateResultClearEveryCycle() |
283
|
|
|
{ |
284
|
|
|
$article1 = new CmsArticle; |
285
|
|
|
$article1->topic = "Doctrine 2"; |
286
|
|
|
$article1->text = "This is an introduction to Doctrine 2."; |
287
|
|
|
|
288
|
|
|
$article2 = new CmsArticle; |
289
|
|
|
$article2->topic = "Symfony 2"; |
290
|
|
|
$article2->text = "This is an introduction to Symfony 2."; |
291
|
|
|
|
292
|
|
|
$this->_em->persist($article1); |
293
|
|
|
$this->_em->persist($article2); |
294
|
|
|
|
295
|
|
|
$this->_em->flush(); |
296
|
|
|
$this->_em->clear(); |
297
|
|
|
|
298
|
|
|
$query = $this->_em->createQuery("select a from Doctrine\Tests\Models\CMS\CmsArticle a"); |
299
|
|
|
$articles = $query->iterate(); |
300
|
|
|
|
301
|
|
|
$iteratedCount = 0; |
302
|
|
|
$topics = []; |
303
|
|
|
foreach($articles AS $row) { |
304
|
|
|
$article = $row[0]; |
305
|
|
|
$topics[] = $article->topic; |
306
|
|
|
|
307
|
|
|
$this->_em->clear(); |
308
|
|
|
|
309
|
|
|
$iteratedCount++; |
310
|
|
|
} |
311
|
|
|
|
312
|
|
|
$this->assertSame(["Doctrine 2", "Symfony 2"], $topics); |
313
|
|
|
$this->assertSame(2, $iteratedCount); |
314
|
|
|
|
315
|
|
|
$this->_em->flush(); |
316
|
|
|
} |
317
|
|
|
|
318
|
|
|
/** |
319
|
|
|
* @expectedException \Doctrine\ORM\Query\QueryException |
320
|
|
|
*/ |
321
|
|
|
public function testIterateResult_FetchJoinedCollection_ThrowsException() |
322
|
|
|
{ |
323
|
|
|
$query = $this->_em->createQuery("SELECT u, a FROM ' . CmsUser::class . ' u JOIN u.articles a"); |
324
|
|
|
$articles = $query->iterate(); |
|
|
|
|
325
|
|
|
} |
326
|
|
|
|
327
|
|
|
/** |
328
|
|
|
* @expectedException Doctrine\ORM\NoResultException |
329
|
|
|
*/ |
330
|
|
|
public function testGetSingleResultThrowsExceptionOnNoResult() |
331
|
|
|
{ |
332
|
|
|
$this->_em->createQuery("select a from Doctrine\Tests\Models\CMS\CmsArticle a") |
333
|
|
|
->getSingleResult(); |
334
|
|
|
} |
335
|
|
|
|
336
|
|
|
/** |
337
|
|
|
* @expectedException Doctrine\ORM\NoResultException |
338
|
|
|
*/ |
339
|
|
|
public function testGetSingleScalarResultThrowsExceptionOnNoResult() |
340
|
|
|
{ |
341
|
|
|
$this->_em->createQuery("select a from Doctrine\Tests\Models\CMS\CmsArticle a") |
342
|
|
|
->getSingleScalarResult(); |
343
|
|
|
} |
344
|
|
|
|
345
|
|
|
/** |
346
|
|
|
* @expectedException Doctrine\ORM\NonUniqueResultException |
347
|
|
|
*/ |
348
|
|
|
public function testGetSingleScalarResultThrowsExceptionOnNonUniqueResult() |
349
|
|
|
{ |
350
|
|
|
$user = new CmsUser; |
351
|
|
|
$user->name = 'Guilherme'; |
352
|
|
|
$user->username = 'gblanco'; |
353
|
|
|
$user->status = 'developer'; |
354
|
|
|
|
355
|
|
|
$article1 = new CmsArticle; |
356
|
|
|
$article1->topic = "Doctrine 2"; |
357
|
|
|
$article1->text = "This is an introduction to Doctrine 2."; |
358
|
|
|
$user->addArticle($article1); |
359
|
|
|
|
360
|
|
|
$article2 = new CmsArticle; |
361
|
|
|
$article2->topic = "Symfony 2"; |
362
|
|
|
$article2->text = "This is an introduction to Symfony 2."; |
363
|
|
|
$user->addArticle($article2); |
364
|
|
|
|
365
|
|
|
$this->_em->persist($user); |
366
|
|
|
$this->_em->persist($article1); |
367
|
|
|
$this->_em->persist($article2); |
368
|
|
|
|
369
|
|
|
$this->_em->flush(); |
370
|
|
|
$this->_em->clear(); |
371
|
|
|
|
372
|
|
|
$this->_em->createQuery("select a from Doctrine\Tests\Models\CMS\CmsArticle a") |
373
|
|
|
->getSingleScalarResult(); |
374
|
|
|
} |
375
|
|
|
|
376
|
|
|
public function testModifiedLimitQuery() |
377
|
|
|
{ |
378
|
|
View Code Duplication |
for ($i = 0; $i < 5; $i++) { |
|
|
|
|
379
|
|
|
$user = new CmsUser; |
380
|
|
|
$user->name = 'Guilherme' . $i; |
381
|
|
|
$user->username = 'gblanco' . $i; |
382
|
|
|
$user->status = 'developer'; |
383
|
|
|
$this->_em->persist($user); |
384
|
|
|
} |
385
|
|
|
|
386
|
|
|
$this->_em->flush(); |
387
|
|
|
$this->_em->clear(); |
388
|
|
|
|
389
|
|
|
$data = $this->_em->createQuery('SELECT u FROM ' . CmsUser::class . ' u') |
390
|
|
|
->setFirstResult(1) |
391
|
|
|
->setMaxResults(2) |
392
|
|
|
->getResult(); |
393
|
|
|
|
394
|
|
|
$this->assertEquals(2, count($data)); |
395
|
|
|
$this->assertEquals('gblanco1', $data[0]->username); |
396
|
|
|
$this->assertEquals('gblanco2', $data[1]->username); |
397
|
|
|
|
398
|
|
|
$data = $this->_em->createQuery('SELECT u FROM ' . CmsUser::class . ' u') |
399
|
|
|
->setFirstResult(3) |
400
|
|
|
->setMaxResults(2) |
401
|
|
|
->getResult(); |
402
|
|
|
|
403
|
|
|
$this->assertEquals(2, count($data)); |
404
|
|
|
$this->assertEquals('gblanco3', $data[0]->username); |
405
|
|
|
$this->assertEquals('gblanco4', $data[1]->username); |
406
|
|
|
|
407
|
|
|
$data = $this->_em->createQuery('SELECT u FROM ' . CmsUser::class . ' u') |
|
|
|
|
408
|
|
|
->setFirstResult(3) |
409
|
|
|
->setMaxResults(2) |
410
|
|
|
->getScalarResult(); |
411
|
|
|
} |
412
|
|
|
|
413
|
|
|
public function testSupportsQueriesWithEntityNamespaces() |
414
|
|
|
{ |
415
|
|
|
$this->_em->getConfiguration()->addEntityNamespace('CMS', 'Doctrine\Tests\Models\CMS'); |
416
|
|
|
|
417
|
|
|
try { |
418
|
|
|
$query = $this->_em->createQuery('UPDATE CMS:CmsUser u SET u.name = ?1'); |
419
|
|
|
$this->assertEquals('UPDATE cms_users SET name = ?', $query->getSQL()); |
420
|
|
|
$query->free(); |
421
|
|
|
} catch (\Exception $e) { |
422
|
|
|
$this->fail($e->getMessage()); |
423
|
|
|
} |
424
|
|
|
|
425
|
|
|
$this->_em->getConfiguration()->setEntityNamespaces([]); |
426
|
|
|
} |
427
|
|
|
|
428
|
|
|
/** |
429
|
|
|
* @group DDC-604 |
430
|
|
|
*/ |
431
|
|
|
public function testEntityParameters() |
432
|
|
|
{ |
433
|
|
|
$article = new CmsArticle; |
434
|
|
|
$article->topic = "dr. dolittle"; |
435
|
|
|
$article->text = "Once upon a time ..."; |
436
|
|
|
$author = new CmsUser; |
437
|
|
|
$author->name = "anonymous"; |
438
|
|
|
$author->username = "anon"; |
439
|
|
|
$author->status = "here"; |
440
|
|
|
$article->user = $author; |
441
|
|
|
$this->_em->persist($author); |
442
|
|
|
$this->_em->persist($article); |
443
|
|
|
$this->_em->flush(); |
444
|
|
|
$this->_em->clear(); |
445
|
|
|
//$this->_em->getConnection()->getConfiguration()->setSQLLogger(new \Doctrine\DBAL\Logging\EchoSQLLogger); |
|
|
|
|
446
|
|
|
$q = $this->_em->createQuery("select a from Doctrine\Tests\Models\CMS\CmsArticle a where a.topic = :topic and a.user = :user") |
447
|
|
|
->setParameter("user", $this->_em->getReference(CmsUser::class, $author->id)) |
448
|
|
|
->setParameter("topic", "dr. dolittle"); |
449
|
|
|
|
450
|
|
|
$result = $q->getResult(); |
451
|
|
|
$this->assertEquals(1, count($result)); |
452
|
|
|
$this->assertInstanceOf(CmsArticle::class, $result[0]); |
453
|
|
|
$this->assertEquals("dr. dolittle", $result[0]->topic); |
454
|
|
|
$this->assertInstanceOf(Proxy::class, $result[0]->user); |
455
|
|
|
$this->assertFalse($result[0]->user->__isInitialized__); |
456
|
|
|
} |
457
|
|
|
|
458
|
|
|
/** |
459
|
|
|
* @group DDC-952 |
460
|
|
|
*/ |
461
|
|
|
public function testEnableFetchEagerMode() |
462
|
|
|
{ |
463
|
|
|
for ($i = 0; $i < 10; $i++) { |
464
|
|
|
$article = new CmsArticle; |
465
|
|
|
$article->topic = "dr. dolittle"; |
466
|
|
|
$article->text = "Once upon a time ..."; |
467
|
|
|
$author = new CmsUser; |
468
|
|
|
$author->name = "anonymous"; |
469
|
|
|
$author->username = "anon".$i; |
470
|
|
|
$author->status = "here"; |
471
|
|
|
$article->user = $author; |
472
|
|
|
$this->_em->persist($author); |
473
|
|
|
$this->_em->persist($article); |
474
|
|
|
} |
475
|
|
|
$this->_em->flush(); |
476
|
|
|
$this->_em->clear(); |
477
|
|
|
|
478
|
|
|
$articles = $this->_em->createQuery('select a from Doctrine\Tests\Models\CMS\CmsArticle a') |
479
|
|
|
->setFetchMode(CmsArticle::class, 'user', ClassMetadata::FETCH_EAGER) |
480
|
|
|
->getResult(); |
481
|
|
|
|
482
|
|
|
$this->assertEquals(10, count($articles)); |
483
|
|
|
foreach ($articles AS $article) { |
484
|
|
|
$this->assertNotInstanceOf(Proxy::class, $article); |
485
|
|
|
} |
486
|
|
|
} |
487
|
|
|
|
488
|
|
|
/** |
489
|
|
|
* @group DDC-991 |
490
|
|
|
*/ |
491
|
|
|
public function testgetOneOrNullResult() |
492
|
|
|
{ |
493
|
|
|
$user = new CmsUser; |
494
|
|
|
$user->name = 'Guilherme'; |
495
|
|
|
$user->username = 'gblanco'; |
496
|
|
|
$user->status = 'developer'; |
497
|
|
|
$this->_em->persist($user); |
498
|
|
|
$this->_em->flush(); |
499
|
|
|
$this->_em->clear(); |
500
|
|
|
|
501
|
|
|
$query = $this->_em->createQuery("select u from " . CmsUser::class . " u where u.username = 'gblanco'"); |
502
|
|
|
|
503
|
|
|
$fetchedUser = $query->getOneOrNullResult(); |
504
|
|
|
$this->assertInstanceOf(CmsUser::class, $fetchedUser); |
505
|
|
|
$this->assertEquals('gblanco', $fetchedUser->username); |
506
|
|
|
|
507
|
|
|
$query = $this->_em->createQuery("select u.username from " . CmsUser::class . " u where u.username = 'gblanco'"); |
508
|
|
|
$fetchedUsername = $query->getOneOrNullResult(Query::HYDRATE_SINGLE_SCALAR); |
509
|
|
|
$this->assertEquals('gblanco', $fetchedUsername); |
510
|
|
|
} |
511
|
|
|
|
512
|
|
|
/** |
513
|
|
|
* @group DDC-991 |
514
|
|
|
*/ |
515
|
|
View Code Duplication |
public function testgetOneOrNullResultSeveralRows() |
516
|
|
|
{ |
517
|
|
|
$user = new CmsUser; |
518
|
|
|
$user->name = 'Guilherme'; |
519
|
|
|
$user->username = 'gblanco'; |
520
|
|
|
$user->status = 'developer'; |
521
|
|
|
$this->_em->persist($user); |
522
|
|
|
$user = new CmsUser; |
523
|
|
|
$user->name = 'Roman'; |
524
|
|
|
$user->username = 'romanb'; |
525
|
|
|
$user->status = 'developer'; |
526
|
|
|
$this->_em->persist($user); |
527
|
|
|
$this->_em->flush(); |
528
|
|
|
$this->_em->clear(); |
529
|
|
|
|
530
|
|
|
$query = $this->_em->createQuery("select u from Doctrine\Tests\Models\CMS\CmsUser u"); |
531
|
|
|
|
532
|
|
|
$this->expectException(NonUniqueResultException::class); |
533
|
|
|
|
534
|
|
|
$fetchedUser = $query->getOneOrNullResult(); |
|
|
|
|
535
|
|
|
} |
536
|
|
|
|
537
|
|
|
/** |
538
|
|
|
* @group DDC-991 |
539
|
|
|
*/ |
540
|
|
|
public function testgetOneOrNullResultNoRows() |
541
|
|
|
{ |
542
|
|
|
$query = $this->_em->createQuery("select u from Doctrine\Tests\Models\CMS\CmsUser u"); |
543
|
|
|
$this->assertNull($query->getOneOrNullResult()); |
544
|
|
|
|
545
|
|
|
$query = $this->_em->createQuery("select u.username from Doctrine\Tests\Models\CMS\CmsUser u where u.username = 'gblanco'"); |
546
|
|
|
$this->assertNull($query->getOneOrNullResult(Query::HYDRATE_SCALAR)); |
547
|
|
|
} |
548
|
|
|
|
549
|
|
|
/** |
550
|
|
|
* @group DBAL-171 |
551
|
|
|
*/ |
552
|
|
|
public function testParameterOrder() |
553
|
|
|
{ |
554
|
|
|
$user1 = new CmsUser; |
555
|
|
|
$user1->name = 'Benjamin'; |
556
|
|
|
$user1->username = 'beberlei'; |
557
|
|
|
$user1->status = 'developer'; |
558
|
|
|
$this->_em->persist($user1); |
559
|
|
|
|
560
|
|
|
$user2 = new CmsUser; |
561
|
|
|
$user2->name = 'Roman'; |
562
|
|
|
$user2->username = 'romanb'; |
563
|
|
|
$user2->status = 'developer'; |
564
|
|
|
$this->_em->persist($user2); |
565
|
|
|
|
566
|
|
|
$user3 = new CmsUser; |
567
|
|
|
$user3->name = 'Jonathan'; |
568
|
|
|
$user3->username = 'jwage'; |
569
|
|
|
$user3->status = 'developer'; |
570
|
|
|
$this->_em->persist($user3); |
571
|
|
|
|
572
|
|
|
$this->_em->flush(); |
573
|
|
|
$this->_em->clear(); |
574
|
|
|
|
575
|
|
|
$query = $this->_em->createQuery("SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE u.status = :a AND u.id IN (:b)"); |
576
|
|
|
$query->setParameters(new ArrayCollection( |
577
|
|
|
[ |
578
|
|
|
new Parameter('b', [$user1->id, $user2->id, $user3->id]), |
579
|
|
|
new Parameter('a', 'developer') |
580
|
|
|
] |
581
|
|
|
)); |
582
|
|
|
$result = $query->getResult(); |
583
|
|
|
|
584
|
|
|
$this->assertEquals(3, count($result)); |
585
|
|
|
} |
586
|
|
|
|
587
|
|
|
public function testDqlWithAutoInferOfParameters() |
588
|
|
|
{ |
589
|
|
|
$user = new CmsUser; |
590
|
|
|
$user->name = 'Benjamin'; |
591
|
|
|
$user->username = 'beberlei'; |
592
|
|
|
$user->status = 'developer'; |
593
|
|
|
$this->_em->persist($user); |
594
|
|
|
|
595
|
|
|
$user = new CmsUser; |
596
|
|
|
$user->name = 'Roman'; |
597
|
|
|
$user->username = 'romanb'; |
598
|
|
|
$user->status = 'developer'; |
599
|
|
|
$this->_em->persist($user); |
600
|
|
|
|
601
|
|
|
$user = new CmsUser; |
602
|
|
|
$user->name = 'Jonathan'; |
603
|
|
|
$user->username = 'jwage'; |
604
|
|
|
$user->status = 'developer'; |
605
|
|
|
$this->_em->persist($user); |
606
|
|
|
|
607
|
|
|
$this->_em->flush(); |
608
|
|
|
$this->_em->clear(); |
609
|
|
|
|
610
|
|
|
$query = $this->_em->createQuery("SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE u.username IN (?0)"); |
611
|
|
|
$query->setParameter(0, ['beberlei', 'jwage']); |
612
|
|
|
|
613
|
|
|
$users = $query->execute(); |
614
|
|
|
|
615
|
|
|
$this->assertEquals(2, count($users)); |
616
|
|
|
} |
617
|
|
|
|
618
|
|
|
public function testQueryBuilderWithStringWhereClauseContainingOrAndConditionalPrimary() |
619
|
|
|
{ |
620
|
|
|
$qb = $this->_em->createQueryBuilder(); |
621
|
|
|
$qb->select('u') |
622
|
|
|
->from(CmsUser::class, 'u') |
623
|
|
|
->innerJoin('u.articles', 'a') |
624
|
|
|
->where('(u.id = 0) OR (u.id IS NULL)'); |
625
|
|
|
|
626
|
|
|
$query = $qb->getQuery(); |
627
|
|
|
$users = $query->execute(); |
628
|
|
|
|
629
|
|
|
$this->assertEquals(0, count($users)); |
630
|
|
|
} |
631
|
|
|
|
632
|
|
|
public function testQueryWithArrayOfEntitiesAsParameter() |
633
|
|
|
{ |
634
|
|
|
$userA = new CmsUser; |
635
|
|
|
$userA->name = 'Benjamin'; |
636
|
|
|
$userA->username = 'beberlei'; |
637
|
|
|
$userA->status = 'developer'; |
638
|
|
|
$this->_em->persist($userA); |
639
|
|
|
|
640
|
|
|
$userB = new CmsUser; |
641
|
|
|
$userB->name = 'Roman'; |
642
|
|
|
$userB->username = 'romanb'; |
643
|
|
|
$userB->status = 'developer'; |
644
|
|
|
$this->_em->persist($userB); |
645
|
|
|
|
646
|
|
|
$userC = new CmsUser; |
647
|
|
|
$userC->name = 'Jonathan'; |
648
|
|
|
$userC->username = 'jwage'; |
649
|
|
|
$userC->status = 'developer'; |
650
|
|
|
$this->_em->persist($userC); |
651
|
|
|
|
652
|
|
|
$this->_em->flush(); |
653
|
|
|
$this->_em->clear(); |
654
|
|
|
|
655
|
|
|
$query = $this->_em->createQuery("SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE u IN (?0) OR u.username = ?1"); |
656
|
|
|
$query->setParameter(0, [$userA, $userC]); |
657
|
|
|
$query->setParameter(1, 'beberlei'); |
658
|
|
|
|
659
|
|
|
$users = $query->execute(); |
660
|
|
|
|
661
|
|
|
$this->assertEquals(2, count($users)); |
662
|
|
|
} |
663
|
|
|
|
664
|
|
|
public function testQueryWithHiddenAsSelectExpression() |
665
|
|
|
{ |
666
|
|
|
$userA = new CmsUser; |
667
|
|
|
$userA->name = 'Benjamin'; |
668
|
|
|
$userA->username = 'beberlei'; |
669
|
|
|
$userA->status = 'developer'; |
670
|
|
|
$this->_em->persist($userA); |
671
|
|
|
|
672
|
|
|
$userB = new CmsUser; |
673
|
|
|
$userB->name = 'Roman'; |
674
|
|
|
$userB->username = 'romanb'; |
675
|
|
|
$userB->status = 'developer'; |
676
|
|
|
$this->_em->persist($userB); |
677
|
|
|
|
678
|
|
|
$userC = new CmsUser; |
679
|
|
|
$userC->name = 'Jonathan'; |
680
|
|
|
$userC->username = 'jwage'; |
681
|
|
|
$userC->status = 'developer'; |
682
|
|
|
$this->_em->persist($userC); |
683
|
|
|
|
684
|
|
|
$this->_em->flush(); |
685
|
|
|
$this->_em->clear(); |
686
|
|
|
|
687
|
|
|
$query = $this->_em->createQuery("SELECT u, (SELECT COUNT(u2.id) FROM Doctrine\Tests\Models\CMS\CmsUser u2) AS HIDDEN total FROM Doctrine\Tests\Models\CMS\CmsUser u"); |
688
|
|
|
$users = $query->execute(); |
689
|
|
|
|
690
|
|
|
$this->assertEquals(3, count($users)); |
691
|
|
|
$this->assertInstanceOf(CmsUser::class, $users[0]); |
692
|
|
|
} |
693
|
|
|
|
694
|
|
|
/** |
695
|
|
|
* @group DDC-1651 |
696
|
|
|
*/ |
697
|
|
|
public function testSetParameterBindingSingleIdentifierObject() |
698
|
|
|
{ |
699
|
|
|
$userC = new CmsUser; |
700
|
|
|
$userC->name = 'Jonathan'; |
701
|
|
|
$userC->username = 'jwage'; |
702
|
|
|
$userC->status = 'developer'; |
703
|
|
|
$this->_em->persist($userC); |
704
|
|
|
|
705
|
|
|
$this->_em->flush(); |
706
|
|
|
$this->_em->clear(); |
707
|
|
|
|
708
|
|
|
$q = $this->_em->createQuery("SELECT DISTINCT u from Doctrine\Tests\Models\CMS\CmsUser u WHERE u.id = ?1"); |
709
|
|
|
$q->setParameter(1, $userC); |
710
|
|
|
|
711
|
|
|
$this->assertEquals($userC, $q->getParameter(1)->getValue()); |
712
|
|
|
|
713
|
|
|
// Parameter is not converted before, but it should be converted during execution. Test should not fail here |
714
|
|
|
$q->getResult(); |
715
|
|
|
} |
716
|
|
|
|
717
|
|
|
/** |
718
|
|
|
* @group DDC-2319 |
719
|
|
|
*/ |
720
|
|
|
public function testSetCollectionParameterBindingSingleIdentifierObject() |
721
|
|
|
{ |
722
|
|
|
$u1 = new CmsUser; |
723
|
|
|
$u1->name = 'Name1'; |
724
|
|
|
$u1->username = 'username1'; |
725
|
|
|
$u1->status = 'developer'; |
726
|
|
|
$this->_em->persist($u1); |
727
|
|
|
|
728
|
|
|
$u2 = new CmsUser; |
729
|
|
|
$u2->name = 'Name2'; |
730
|
|
|
$u2->username = 'username2'; |
731
|
|
|
$u2->status = 'tester'; |
732
|
|
|
$this->_em->persist($u2); |
733
|
|
|
|
734
|
|
|
$u3 = new CmsUser; |
735
|
|
|
$u3->name = 'Name3'; |
736
|
|
|
$u3->username = 'username3'; |
737
|
|
|
$u3->status = 'tester'; |
738
|
|
|
$this->_em->persist($u3); |
739
|
|
|
|
740
|
|
|
$this->_em->flush(); |
741
|
|
|
$this->_em->clear(); |
742
|
|
|
|
743
|
|
|
$userCollection = new ArrayCollection(); |
744
|
|
|
|
745
|
|
|
$userCollection->add($u1); |
746
|
|
|
$userCollection->add($u2); |
747
|
|
|
$userCollection->add($u3->getId()); |
748
|
|
|
|
749
|
|
|
$q = $this->_em->createQuery("SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE u IN (:users) ORDER BY u.id"); |
750
|
|
|
$q->setParameter('users', $userCollection); |
751
|
|
|
$users = $q->execute(); |
752
|
|
|
|
753
|
|
|
$this->assertEquals(3, count($users)); |
754
|
|
|
$this->assertInstanceOf(CmsUser::class, $users[0]); |
755
|
|
|
$this->assertInstanceOf(CmsUser::class, $users[1]); |
756
|
|
|
$this->assertInstanceOf(CmsUser::class, $users[2]); |
757
|
|
|
|
758
|
|
|
$resultUser1 = $users[0]; |
759
|
|
|
$resultUser2 = $users[1]; |
760
|
|
|
$resultUser3 = $users[2]; |
761
|
|
|
|
762
|
|
|
$this->assertEquals($u1->username, $resultUser1->username); |
763
|
|
|
$this->assertEquals($u2->username, $resultUser2->username); |
764
|
|
|
$this->assertEquals($u3->username, $resultUser3->username); |
765
|
|
|
} |
766
|
|
|
|
767
|
|
|
/** |
768
|
|
|
* @group DDC-1822 |
769
|
|
|
*/ |
770
|
|
|
public function testUnexpectedResultException() |
771
|
|
|
{ |
772
|
|
|
$dql = "SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u"; |
773
|
|
|
$u1 = new CmsUser; |
774
|
|
|
$u2 = new CmsUser; |
775
|
|
|
$u1->name = 'Fabio B. Silva'; |
776
|
|
|
$u1->username = 'FabioBatSilva'; |
777
|
|
|
$u1->status = 'developer'; |
778
|
|
|
$u2->name = 'Test'; |
779
|
|
|
$u2->username = 'test'; |
780
|
|
|
$u2->status = 'tester'; |
781
|
|
|
|
782
|
|
|
try { |
783
|
|
|
$this->_em->createQuery($dql)->getSingleResult(); |
784
|
|
|
$this->fail('Expected exception "\Doctrine\ORM\NoResultException".'); |
785
|
|
|
} catch (UnexpectedResultException $exc) { |
786
|
|
|
$this->assertInstanceOf('\Doctrine\ORM\NoResultException', $exc); |
787
|
|
|
} |
788
|
|
|
|
789
|
|
|
|
790
|
|
|
$this->_em->persist($u1); |
791
|
|
|
$this->_em->persist($u2); |
792
|
|
|
$this->_em->flush(); |
793
|
|
|
$this->_em->clear(); |
794
|
|
|
|
795
|
|
|
try { |
796
|
|
|
$this->_em->createQuery($dql)->getSingleResult(); |
797
|
|
|
$this->fail('Expected exception "\Doctrine\ORM\NonUniqueResultException".'); |
798
|
|
|
} catch (UnexpectedResultException $exc) { |
799
|
|
|
$this->assertInstanceOf('\Doctrine\ORM\NonUniqueResultException', $exc); |
800
|
|
|
} |
801
|
|
|
} |
802
|
|
|
|
803
|
|
|
public function testMultipleJoinComponentsUsingInnerJoin() |
804
|
|
|
{ |
805
|
|
|
$userA = new CmsUser; |
806
|
|
|
$userA->name = 'Benjamin'; |
807
|
|
|
$userA->username = 'beberlei'; |
808
|
|
|
$userA->status = 'developer'; |
809
|
|
|
|
810
|
|
|
$phonenumberA = new CmsPhonenumber; |
811
|
|
|
$phonenumberA->phonenumber = '111111'; |
812
|
|
|
$userA->addPhonenumber($phonenumberA); |
813
|
|
|
|
814
|
|
|
$userB = new CmsUser; |
815
|
|
|
$userB->name = 'Alexander'; |
816
|
|
|
$userB->username = 'asm89'; |
817
|
|
|
$userB->status = 'developer'; |
818
|
|
|
|
819
|
|
|
$this->_em->persist($userA); |
820
|
|
|
$this->_em->persist($userB); |
821
|
|
|
$this->_em->flush(); |
822
|
|
|
$this->_em->clear(); |
823
|
|
|
|
824
|
|
|
$query = $this->_em->createQuery(" |
825
|
|
|
SELECT u, p |
826
|
|
|
FROM Doctrine\Tests\Models\CMS\CmsUser u |
827
|
|
|
INNER JOIN Doctrine\Tests\Models\CMS\CmsPhonenumber p WITH u = p.user |
828
|
|
|
"); |
829
|
|
|
$users = $query->execute(); |
830
|
|
|
|
831
|
|
|
$this->assertEquals(2, count($users)); |
832
|
|
|
$this->assertInstanceOf(CmsUser::class, $users[0]); |
833
|
|
|
$this->assertInstanceOf(CmsPhonenumber::class, $users[1]); |
834
|
|
|
} |
835
|
|
|
|
836
|
|
|
public function testMultipleJoinComponentsUsingLeftJoin() |
837
|
|
|
{ |
838
|
|
|
$userA = new CmsUser; |
839
|
|
|
$userA->name = 'Benjamin'; |
840
|
|
|
$userA->username = 'beberlei'; |
841
|
|
|
$userA->status = 'developer'; |
842
|
|
|
|
843
|
|
|
$phonenumberA = new CmsPhonenumber; |
844
|
|
|
$phonenumberA->phonenumber = '111111'; |
845
|
|
|
$userA->addPhonenumber($phonenumberA); |
846
|
|
|
|
847
|
|
|
$userB = new CmsUser; |
848
|
|
|
$userB->name = 'Alexander'; |
849
|
|
|
$userB->username = 'asm89'; |
850
|
|
|
$userB->status = 'developer'; |
851
|
|
|
|
852
|
|
|
$this->_em->persist($userA); |
853
|
|
|
$this->_em->persist($userB); |
854
|
|
|
$this->_em->flush(); |
855
|
|
|
$this->_em->clear(); |
856
|
|
|
|
857
|
|
|
$query = $this->_em->createQuery(" |
858
|
|
|
SELECT u, p |
859
|
|
|
FROM Doctrine\Tests\Models\CMS\CmsUser u |
860
|
|
|
LEFT JOIN Doctrine\Tests\Models\CMS\CmsPhonenumber p WITH u = p.user |
861
|
|
|
"); |
862
|
|
|
$users = $query->execute(); |
863
|
|
|
|
864
|
|
|
$this->assertEquals(4, count($users)); |
865
|
|
|
$this->assertInstanceOf(CmsUser::class, $users[0]); |
866
|
|
|
$this->assertInstanceOf(CmsPhonenumber::class, $users[1]); |
867
|
|
|
$this->assertInstanceOf(CmsUser::class, $users[2]); |
868
|
|
|
$this->assertNull($users[3]); |
869
|
|
|
} |
870
|
|
|
} |
871
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.