forked from doctrine/orm
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'origin/2.19.x' into 3.2.x
- Loading branch information
Showing
5 changed files
with
121 additions
and
4 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
24 changes: 24 additions & 0 deletions
24
tests/Tests/Models/CompositeKeyRelations/CustomerClass.php
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 |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Doctrine\Tests\Models\CompositeKeyRelations; | ||
|
||
use Doctrine\ORM\Mapping\Column; | ||
use Doctrine\ORM\Mapping\Entity; | ||
use Doctrine\ORM\Mapping\Id; | ||
|
||
#[Entity] | ||
class CustomerClass | ||
{ | ||
#[Id] | ||
#[Column(type: 'string')] | ||
public string $companyCode; | ||
|
||
#[Id] | ||
#[Column(type: 'string')] | ||
public string $code; | ||
|
||
#[Column(type: 'string')] | ||
public string $name; | ||
} |
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 |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Doctrine\Tests\Models\CompositeKeyRelations; | ||
|
||
use Doctrine\ORM\Mapping\Column; | ||
use Doctrine\ORM\Mapping\Entity; | ||
use Doctrine\ORM\Mapping\Id; | ||
use Doctrine\ORM\Mapping\JoinColumn; | ||
use Doctrine\ORM\Mapping\ManyToOne; | ||
|
||
#[Entity] | ||
class InvoiceClass | ||
{ | ||
#[Id] | ||
#[Column(type: 'string')] | ||
public string $companyCode; | ||
|
||
#[Id] | ||
#[Column(type: 'string')] | ||
public string $invoiceNumber; | ||
|
||
#[ManyToOne(targetEntity: CustomerClass::class)] | ||
#[JoinColumn(name: 'companyCode', referencedColumnName: 'companyCode')] | ||
#[JoinColumn(name: 'customerCode', referencedColumnName: 'code')] | ||
public CustomerClass|null $customer; | ||
|
||
#[Column(type: 'string', nullable: true)] | ||
public string|null $customerCode = null; | ||
} |
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 |
---|---|---|
@@ -0,0 +1,61 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Doctrine\Tests\ORM\Functional; | ||
|
||
use Doctrine\Tests\Models\CompositeKeyRelations\CustomerClass; | ||
use Doctrine\Tests\Models\CompositeKeyRelations\InvoiceClass; | ||
use Doctrine\Tests\OrmFunctionalTestCase; | ||
|
||
class CompositeKeyRelationsTest extends OrmFunctionalTestCase | ||
{ | ||
protected function setUp(): void | ||
{ | ||
$this->useModelSet('compositekeyrelations'); | ||
|
||
parent::setUp(); | ||
} | ||
|
||
public function testFindEntityWithNotNullRelation(): void | ||
{ | ||
$this->_em->getConnection()->insert('CustomerClass', [ | ||
'companyCode' => 'AA', | ||
'code' => 'CUST1', | ||
'name' => 'Customer 1', | ||
]); | ||
|
||
$this->_em->getConnection()->insert('InvoiceClass', [ | ||
'companyCode' => 'AA', | ||
'invoiceNumber' => 'INV1', | ||
'customerCode' => 'CUST1', | ||
]); | ||
|
||
$entity = $this->findEntity('AA', 'INV1'); | ||
self::assertSame('AA', $entity->companyCode); | ||
self::assertSame('INV1', $entity->invoiceNumber); | ||
self::assertInstanceOf(CustomerClass::class, $entity->customer); | ||
self::assertSame('Customer 1', $entity->customer->name); | ||
} | ||
|
||
public function testFindEntityWithNullRelation(): void | ||
{ | ||
$this->_em->getConnection()->insert('InvoiceClass', [ | ||
'companyCode' => 'BB', | ||
'invoiceNumber' => 'INV1', | ||
]); | ||
|
||
$entity = $this->findEntity('BB', 'INV1'); | ||
self::assertSame('BB', $entity->companyCode); | ||
self::assertSame('INV1', $entity->invoiceNumber); | ||
self::assertNull($entity->customer); | ||
} | ||
|
||
private function findEntity(string $companyCode, string $invoiceNumber): InvoiceClass | ||
{ | ||
return $this->_em->find( | ||
InvoiceClass::class, | ||
['companyCode' => $companyCode, 'invoiceNumber' => $invoiceNumber], | ||
); | ||
} | ||
} |
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