Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fetching entities with Composite Key Relations #8425

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions lib/Doctrine/ORM/UnitOfWork.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
use Throwable;
use UnexpectedValueException;
use function get_class;
use function in_array;
use function is_object;
use function spl_object_hash;

Expand Down Expand Up @@ -2761,9 +2762,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
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

declare(strict_types=1);

namespace Doctrine\Tests\Models\CompositeKeyRelations;

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

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

/**
* @Column(type="string")
*/
public string $name;
}
35 changes: 35 additions & 0 deletions tests/Doctrine/Tests/Models/CompositeKeyRelations/InvoiceClass.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

declare(strict_types=1);

namespace Doctrine\Tests\Models\CompositeKeyRelations;

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

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

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

/**
* @Column(type="string", nullable=true)
*/
public ?string $customerCode;
}
60 changes: 60 additions & 0 deletions tests/Doctrine/Tests/ORM/Functional/CompositeKeyRelationsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?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/Doctrine/Tests/OrmFunctionalTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,10 @@ abstract class OrmFunctionalTestCase extends OrmTestCase
Models\CustomType\CustomTypeParent::class,
Models\CustomType\CustomTypeUpperCase::class,
],
'compositekeyrelations' => [
Models\CompositeKeyRelations\InvoiceClass::class,
Models\CompositeKeyRelations\CustomerClass::class,
],
'compositekeyinheritance' => [
Models\CompositeKeyInheritance\JoinedRootClass::class,
Models\CompositeKeyInheritance\JoinedChildClass::class,
Expand Down