-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #10774 from greg0ire/document-dto-in-rsm
- Loading branch information
Showing
2 changed files
with
106 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,6 +19,7 @@ | |
use Doctrine\Tests\Models\CMS\CmsEmail; | ||
use Doctrine\Tests\Models\CMS\CmsPhonenumber; | ||
use Doctrine\Tests\Models\CMS\CmsUser; | ||
use Doctrine\Tests\Models\CMS\CmsUserDTO; | ||
use Doctrine\Tests\Models\Company\CompanyContract; | ||
use Doctrine\Tests\Models\Company\CompanyEmployee; | ||
use Doctrine\Tests\Models\Company\CompanyFixContract; | ||
|
@@ -155,6 +156,77 @@ public function testJoinedOneToManyNativeQuery(): void | |
self::assertSame($phones[0]->getUser(), $users[0]); | ||
} | ||
|
||
public function testMappingAsDto(): void | ||
{ | ||
$user = new CmsUser(); | ||
$user->name = 'Roman'; | ||
$user->username = 'romanb'; | ||
$user->status = 'dev'; | ||
|
||
$phone = new CmsPhonenumber(); | ||
$phone->phonenumber = 424242; | ||
|
||
$user->addPhonenumber($phone); | ||
|
||
$email = new CmsEmail(); | ||
$email->email = '[email protected]'; | ||
|
||
$user->setEmail($email); | ||
|
||
$addr = new CmsAddress(); | ||
$addr->country = 'germany'; | ||
$addr->zip = 10827; | ||
$addr->city = 'Berlin'; | ||
|
||
$user->setAddress($addr); | ||
|
||
$this->_em->persist($user); | ||
$this->_em->flush(); | ||
|
||
$this->_em->clear(); | ||
|
||
$rsm = new ResultSetMapping(); | ||
$rsm->addScalarResult('name', 1, 'string'); | ||
$rsm->addScalarResult('email', 2, 'string'); | ||
$rsm->addScalarResult('city', 3, 'string'); | ||
$rsm->newObjectMappings['name'] = [ | ||
'className' => CmsUserDTO::class, | ||
'objIndex' => 0, | ||
'argIndex' => 0, | ||
]; | ||
$rsm->newObjectMappings['email'] = [ | ||
'className' => CmsUserDTO::class, | ||
'objIndex' => 0, | ||
'argIndex' => 1, | ||
]; | ||
$rsm->newObjectMappings['city'] = [ | ||
'className' => CmsUserDTO::class, | ||
'objIndex' => 0, | ||
'argIndex' => 2, | ||
]; | ||
$query = $this->_em->createNativeQuery( | ||
<<<'SQL' | ||
SELECT u.name, e.email, a.city | ||
FROM cms_users u | ||
INNER JOIN cms_phonenumbers p ON u.id = p.user_id | ||
INNER JOIN cms_emails e ON e.id = u.email_id | ||
INNER JOIN cms_addresses a ON u.id = a.user_id | ||
WHERE username = ? | ||
SQL | ||
, | ||
$rsm | ||
); | ||
$query->setParameter(1, 'romanb'); | ||
|
||
$users = $query->getResult(); | ||
self::assertCount(1, $users); | ||
$user = $users[0]; | ||
self::assertInstanceOf(CmsUserDTO::class, $user); | ||
self::assertEquals('Roman', $user->name); | ||
self::assertEquals('[email protected]', $user->email); | ||
self::assertEquals('Berlin', $user->address); | ||
} | ||
|
||
public function testJoinedOneToOneNativeQuery(): void | ||
{ | ||
$user = new CmsUser(); | ||
|