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

Fix attribute ManyToMany mapping #10671

Merged
Merged
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
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);
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've looked into the issue, and it looks like there is a piece of code that is just missing from this new implementation. I don't know why it was done like that, but your commit message could look like this:

Take into account join columns specifications.

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

Copy link
Member

@greg0ire greg0ire May 4, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like some other stuff was forgotten in #8266 in this code block: d7d6b9d

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It was missing because php 8.0 does not support nested attributes

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah great! That explains it fully!

}

foreach ($this->reader->getPropertyAttributeCollection($property, Mapping\JoinColumn::class) as $joinColumn) {
Expand Down
42 changes: 41 additions & 1 deletion tests/Doctrine/Tests/ORM/Mapping/AttributeDriverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@
namespace Doctrine\Tests\ORM\Mapping;

use Attribute;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\ORM\Mapping\Driver\AttributeDriver;
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 @@ -123,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 All @@ -136,7 +176,7 @@ class AttributeEntityWithoutOriginalParents
#[ORM\GeneratedValue]
public $id;

/** @var AttributeEntityWithoutOriginalParents[] */
/** @var Collection<AttributeEntityWithoutOriginalParents> */
#[ORM\ManyToMany(targetEntity: self::class)]
#[ORM\JoinColumn(name: 'assoz_id', referencedColumnName: 'assoz_id')]
#[ORM\InverseJoinColumn(name: 'assoz_id', referencedColumnName: 'assoz_id')]
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;
}