@@ -401,13 +401,128 @@ public function testToIterableWithMultipleSelectElements(): void
401401 }
402402 }
403403
404- public function testToIterableWithMixedResultIsNotAllowed (): void
404+ public function testToIterableWithMixedResultEntityScalars (): void
405405 {
406- $ this ->expectException (QueryException::class);
407- $ this ->expectExceptionMessage ('Iterating a query with mixed results (using scalars) is not supported. ' );
406+ $ author = new CmsUser ();
407+ $ author ->name = 'Ben ' ;
408+ $ author ->username = 'beberlei ' ;
408409
409- $ query = $ this ->_em ->createQuery ('select a, a.topic from ' . CmsArticle::class . ' a ' );
410- $ query ->toIterable ();
410+ $ article1 = new CmsArticle ();
411+ $ article1 ->topic = 'Doctrine 2 ' ;
412+ $ article1 ->text = 'This is an introduction to Doctrine 2. ' ;
413+ $ article1 ->setAuthor ($ author );
414+
415+ $ article2 = new CmsArticle ();
416+ $ article2 ->topic = 'Symfony 2 ' ;
417+ $ article2 ->text = 'This is an introduction to Symfony 2. ' ;
418+ $ article2 ->setAuthor ($ author );
419+
420+ $ article3 = new CmsArticle ();
421+ $ article3 ->topic = 'lala 2 ' ;
422+ $ article3 ->text = 'This is an introduction to Symfony 2. ' ;
423+ $ article3 ->setAuthor ($ author );
424+
425+ $ this ->_em ->persist ($ article1 );
426+ $ this ->_em ->persist ($ article2 );
427+ $ this ->_em ->persist ($ article3 );
428+ $ this ->_em ->persist ($ author );
429+
430+ $ this ->_em ->flush ();
431+ $ this ->_em ->clear ();
432+
433+ $ query = $ this ->_em ->createQuery ('select a, a.topic, a.text from ' . CmsArticle::class . ' a ' );
434+ $ result = $ query ->toIterable ();
435+
436+ $ it = iterator_to_array ($ result );
437+ $ this ->assertCount (3 , $ it );
438+ $ this ->assertEquals ('Doctrine 2 ' , $ it [0 ]['topic ' ]);
439+ $ this ->assertEquals ('Doctrine 2 ' , $ it [0 ][0 ]->topic );
440+ $ this ->assertEquals ('lala 2 ' , $ it [2 ]['topic ' ]);
441+ $ this ->assertEquals ('lala 2 ' , $ it [2 ][0 ]->topic );
442+ }
443+
444+ public function testToIterableWithMixedResultArbitraryJoinsScalars (): void
445+ {
446+ $ author = new CmsUser ();
447+ $ author ->name = 'Ben ' ;
448+ $ author ->username = 'beberlei ' ;
449+
450+ $ article1 = new CmsArticle ();
451+ $ article1 ->topic = 'Doctrine 2 ' ;
452+ $ article1 ->text = 'This is an introduction to Doctrine 2. ' ;
453+ $ article1 ->setAuthor ($ author );
454+
455+ $ article2 = new CmsArticle ();
456+ $ article2 ->topic = 'Symfony 2 ' ;
457+ $ article2 ->text = 'This is an introduction to Symfony 2. ' ;
458+ $ article2 ->setAuthor ($ author );
459+
460+ $ article3 = new CmsArticle ();
461+ $ article3 ->topic = 'lala 2 ' ;
462+ $ article3 ->text = 'This is an introduction to Symfony 2. ' ;
463+ $ article3 ->setAuthor ($ author );
464+
465+ $ this ->_em ->persist ($ article1 );
466+ $ this ->_em ->persist ($ article2 );
467+ $ this ->_em ->persist ($ article3 );
468+ $ this ->_em ->persist ($ author );
469+
470+ $ this ->_em ->flush ();
471+ $ this ->_em ->clear ();
472+
473+ $ query = $ this ->_em ->createQuery ('select a, u, a.topic, a.text from ' . CmsArticle::class . ' a, ' . CmsUser::class . ' u WHERE a.user = u ' );
474+ $ result = $ query ->toIterable ();
475+
476+ $ it = iterator_to_array ($ result );
477+
478+ $ this ->assertCount (3 , $ it );
479+ $ this ->assertEquals ('Doctrine 2 ' , $ it [0 ]['topic ' ]);
480+ $ this ->assertEquals ('Doctrine 2 ' , $ it [0 ][0 ]->topic );
481+ $ this ->assertEquals ('beberlei ' , $ it [0 ][1 ]->username );
482+ $ this ->assertEquals ('lala 2 ' , $ it [2 ]['topic ' ]);
483+ $ this ->assertEquals ('lala 2 ' , $ it [2 ][0 ]->topic );
484+ $ this ->assertEquals ('beberlei ' , $ it [2 ][1 ]->username );
485+ }
486+
487+ public function testToIterableWithMixedResultScalarsOnly (): void
488+ {
489+ $ author = new CmsUser ();
490+ $ author ->name = 'Ben ' ;
491+ $ author ->username = 'beberlei ' ;
492+
493+ $ article1 = new CmsArticle ();
494+ $ article1 ->topic = 'Doctrine 2 ' ;
495+ $ article1 ->text = 'This is an introduction to Doctrine 2. ' ;
496+ $ article1 ->setAuthor ($ author );
497+
498+ $ article2 = new CmsArticle ();
499+ $ article2 ->topic = 'Symfony 2 ' ;
500+ $ article2 ->text = 'This is an introduction to Symfony 2. ' ;
501+ $ article2 ->setAuthor ($ author );
502+
503+ $ article3 = new CmsArticle ();
504+ $ article3 ->topic = 'lala 2 ' ;
505+ $ article3 ->text = 'This is an introduction to Symfony 2. ' ;
506+ $ article3 ->setAuthor ($ author );
507+
508+ $ this ->_em ->persist ($ article1 );
509+ $ this ->_em ->persist ($ article2 );
510+ $ this ->_em ->persist ($ article3 );
511+ $ this ->_em ->persist ($ author );
512+
513+ $ this ->_em ->flush ();
514+ $ this ->_em ->clear ();
515+
516+ $ query = $ this ->_em ->createQuery ('select a.topic, a.text from ' . CmsArticle::class . ' a ' );
517+ $ result = $ query ->toIterable ();
518+
519+ $ it = iterator_to_array ($ result );
520+
521+ $ this ->assertEquals ([
522+ ['topic ' => 'Doctrine 2 ' , 'text ' => 'This is an introduction to Doctrine 2. ' ],
523+ ['topic ' => 'Symfony 2 ' , 'text ' => 'This is an introduction to Symfony 2. ' ],
524+ ['topic ' => 'lala 2 ' , 'text ' => 'This is an introduction to Symfony 2. ' ],
525+ ], $ it );
411526 }
412527
413528 public function testIterateResultClearEveryCycle (): void
0 commit comments