Skip to content

Commit

Permalink
Merge pull request #11531 from doctrine/2.19.x-merge-up-into-2.20.x_Q…
Browse files Browse the repository at this point in the history
…MtlHSin

Merge release 2.19.6 into 2.20.x
  • Loading branch information
greg0ire authored Jun 27, 2024
2 parents d0e9177 + c1bb2cc commit 7d01f19
Show file tree
Hide file tree
Showing 6 changed files with 147 additions and 6 deletions.
4 changes: 2 additions & 2 deletions docs/en/tutorials/getting-started.rst
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ step:
// Create a simple "default" Doctrine ORM configuration for Attributes
$config = ORMSetup::createAttributeMetadataConfiguration(
paths: array(__DIR__."/src"),
paths: [__DIR__ . '/src'],
isDevMode: true,
);
// or if you prefer annotation, YAML or XML
Expand All @@ -153,7 +153,7 @@ step:
// isDevMode: true,
// );
// $config = ORMSetup::createXMLMetadataConfiguration(
// paths: array(__DIR__."/config/xml"),
// paths: [__DIR__ . '/config/xml'],
// isDevMode: true,
//);
// $config = ORMSetup::createYAMLMetadataConfiguration(
Expand Down
5 changes: 1 addition & 4 deletions src/UnitOfWork.php
Original file line number Diff line number Diff line change
Expand Up @@ -3063,10 +3063,7 @@ public function createEntity($className, array $data, &$hints = [])
} else {
$associatedId[$targetClass->fieldNames[$targetColumn]] = $joinColumnValue;
}
} elseif (
$targetClass->containsForeignIdentifier
&& in_array($targetClass->getFieldForColumn($targetColumn), $targetClass->identifier, true)
) {
} elseif (in_array($targetClass->getFieldForColumn($targetColumn), $targetClass->identifier, true)) {
// the missing key is part of target's entity primary key
$associatedId = [];
break;
Expand Down
33 changes: 33 additions & 0 deletions tests/Tests/Models/CompositeKeyRelations/CustomerClass.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?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
{
/**
* @var string
* @Id
* @Column(type="string")
*/
public $companyCode;

/**
* @var string
* @Id
* @Column(type="string")
*/
public $code;

/**
* @var string
* @Column(type="string")
*/
public $name;
}
46 changes: 46 additions & 0 deletions tests/Tests/Models/CompositeKeyRelations/InvoiceClass.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?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\JoinColumns;
use Doctrine\ORM\Mapping\ManyToOne;

/** @Entity */
class InvoiceClass
{
/**
* @var string
* @Id
* @Column(type="string")
*/
public $companyCode;

/**
* @var string
* @Id
* @Column(type="string")
*/
public $invoiceNumber;

/**
* @var CustomerClass|null
* @ManyToOne(targetEntity="CustomerClass")
* @JoinColumns({
* @JoinColumn(name="companyCode", referencedColumnName="companyCode"),
* @JoinColumn(name="customerCode", referencedColumnName="code")
* })
*/
public $customer;

/**
* @var string|null
* @Column(type="string", nullable=true)
*/
public $customerCode;
}
61 changes: 61 additions & 0 deletions tests/Tests/ORM/Functional/CompositeKeyRelationsTest.php
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]
);
}
}
4 changes: 4 additions & 0 deletions tests/Tests/OrmFunctionalTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,10 @@ abstract class OrmFunctionalTestCase extends OrmTestCase
Models\CompositeKeyInheritance\SingleRootClass::class,
Models\CompositeKeyInheritance\SingleChildClass::class,
],
'compositekeyrelations' => [
Models\CompositeKeyRelations\InvoiceClass::class,
Models\CompositeKeyRelations\CustomerClass::class,
],
'taxi' => [
Models\Taxi\PaidRide::class,
Models\Taxi\Ride::class,
Expand Down

0 comments on commit 7d01f19

Please sign in to comment.