Skip to content

Commit

Permalink
Take into account join columns specifications.
Browse files Browse the repository at this point in the history
Currently, the AttributeDriver ignores any join column attribute specified on a many to many relationship.
Let's copy code from the AnnotationDriver to fix that.

Fixes #9902
  • Loading branch information
BoShurik committed May 4, 2023
1 parent b48dafd commit 2af6e4d
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 0 deletions.
8 changes: 8 additions & 0 deletions lib/Doctrine/ORM/Mapping/Driver/AttributeDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -433,6 +433,14 @@ public function loadMetadataForClass($className, PersistenceClassMetadata $metad
if ($joinTableAttribute->options) {
$joinTable['options'] = $joinTableAttribute->options;
}

foreach ($joinTableAttribute->joinColumns as $joinColumn) {
$joinTable['joinColumns'][] = $this->joinColumnToArray($joinColumn);
}

foreach ($joinTableAttribute->inverseJoinColumns as $joinColumn) {
$joinTable['inverseJoinColumns'][] = $this->joinColumnToArray($joinColumn);
}
}

foreach ($this->reader->getPropertyAttributeCollection($property, Mapping\JoinColumn::class) as $joinColumn) {
Expand Down
39 changes: 39 additions & 0 deletions tests/Doctrine/Tests/ORM/Mapping/AttributeDriverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Doctrine\ORM\Mapping\MappingAttribute;
use Doctrine\Persistence\Mapping\Driver\AnnotationDriver as PersistenceAnnotationDriver;
use Doctrine\Persistence\Mapping\Driver\MappingDriver;
use Doctrine\Tests\ORM\Mapping\Fixtures\AttributeEntityWithNestedJoinColumns;
use stdClass;

use function class_exists;
Expand Down Expand Up @@ -124,6 +125,44 @@ public function testLegacyInheritance(): void

self::assertTrue(is_subclass_of(AttributeDriver::class, PersistenceAnnotationDriver::class));
}

/**
* @requires PHP 8.1
*/
public function testManyToManyAssociationWithNestedJoinColumns(): void
{
$factory = $this->createClassMetadataFactory();

$metadata = $factory->getMetadataFor(AttributeEntityWithNestedJoinColumns::class);

self::assertEquals(
[
[
'name' => 'assoz_id',
'referencedColumnName' => 'assoz_id',
'unique' => false,
'nullable' => true,
'onDelete' => null,
'columnDefinition' => null,
],
],
$metadata->associationMappings['assoc']['joinTable']['joinColumns']
);

self::assertEquals(
[
[
'name' => 'inverse_assoz_id',
'referencedColumnName' => 'inverse_assoz_id',
'unique' => false,
'nullable' => true,
'onDelete' => null,
'columnDefinition' => null,
],
],
$metadata->associationMappings['assoc']['joinTable']['inverseJoinColumns']
);
}
}

#[ORM\Entity]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

declare(strict_types=1);

namespace Doctrine\Tests\ORM\Mapping\Fixtures;

use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;

#[ORM\Entity]
class AttributeEntityWithNestedJoinColumns
{
/** @var int */
#[ORM\Id]
#[ORM\Column(type: 'integer')]
#[ORM\GeneratedValue]
public $id;

/** @var Collection<AttributeEntityWithNestedJoinColumns> */
#[ORM\ManyToMany(targetEntity: self::class)]
#[ORM\JoinTable(
name: 'assoc_table',
joinColumns: new ORM\JoinColumn(name: 'assoz_id', referencedColumnName: 'assoz_id'),
inverseJoinColumns: new ORM\JoinColumn(name: 'inverse_assoz_id', referencedColumnName: 'inverse_assoz_id')
)]
public $assoc;
}

0 comments on commit 2af6e4d

Please sign in to comment.