Skip to content

Commit

Permalink
Make the join column name mandatory
Browse files Browse the repository at this point in the history
  • Loading branch information
greg0ire committed May 28, 2023
1 parent 8a6dbb4 commit ee62934
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 21 deletions.
4 changes: 2 additions & 2 deletions lib/Doctrine/ORM/Mapping/JoinColumnMapping.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ final class JoinColumnMapping implements ArrayAccess
{
use ArrayAccessImplementation;

public string|null $name = null;
public bool|null $unique = null;
public bool|null $quoted = null;
public string|null $fieldName = null;
Expand All @@ -25,6 +24,7 @@ final class JoinColumnMapping implements ArrayAccess
public array|null $options = null;

public function __construct(
public string $name,
public string $referencedColumnName,
) {
}
Expand All @@ -35,7 +35,7 @@ public function __construct(
*/
public static function fromMappingArray(array $mappingArray): self
{
$mapping = new self($mappingArray['referencedColumnName']);
$mapping = new self($mappingArray['name'], $mappingArray['referencedColumnName']);
foreach ($mappingArray as $key => $value) {
if (property_exists($mapping, $key) && $value !== null) {
$mapping->$key = $value;
Expand Down
32 changes: 23 additions & 9 deletions lib/Doctrine/ORM/Mapping/ManyToManyOwningSideMapping.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,30 @@ public function toArray(): array
*/
public static function fromMappingArrayAndNamingStrategy(array $mappingArray, NamingStrategy $namingStrategy): self
{
if (isset($mappingArray['joinTable']['joinColumns'])) {
foreach ($mappingArray['joinTable']['joinColumns'] as $key => $joinColumn) {
if (empty($joinColumn['name'])) {
$mappingArray['joinTable']['joinColumns'][$key]['name'] = $namingStrategy->joinKeyColumnName(
$mappingArray['sourceEntity'],
$joinColumn['referencedColumnName'] ?? null,
);
}
}
}

if (isset($mappingArray['joinTable']['inverseJoinColumns'])) {
foreach ($mappingArray['joinTable']['inverseJoinColumns'] as $key => $joinColumn) {
if (empty($joinColumn['name'])) {
$mappingArray['joinTable']['inverseJoinColumns'][$key]['name'] = $namingStrategy->joinKeyColumnName(
$mappingArray['targetEntity'],
$joinColumn['referencedColumnName'] ?? null,
);
}
}
}

// owning side MUST have a join table
if (! isset($mappingArray['joinTable']['name'])) {
if (! isset($mappingArray['joinTable']) || ! isset($mappingArray['joinTable']['name'])) {
$mappingArray['joinTable']['name'] = $namingStrategy->joinTableName(
$mappingArray['sourceEntity'],
$mappingArray['targetEntity'],
Expand Down Expand Up @@ -85,10 +107,6 @@ public static function fromMappingArrayAndNamingStrategy(array $mappingArray, Na
$mapping->joinTableColumns = [];

foreach ($mapping->joinTable->joinColumns as $joinColumn) {
if (empty($joinColumn->name)) {
$joinColumn->name = $namingStrategy->joinKeyColumnName($mapping->sourceEntity, $joinColumn->referencedColumnName);
}

if (empty($joinColumn->referencedColumnName)) {
$joinColumn->referencedColumnName = $namingStrategy->referenceColumnName();
}
Expand All @@ -112,10 +130,6 @@ public static function fromMappingArrayAndNamingStrategy(array $mappingArray, Na
}

foreach ($mapping->joinTable->inverseJoinColumns as $inverseJoinColumn) {
if (empty($inverseJoinColumn->name)) {
$inverseJoinColumn->name = $namingStrategy->joinKeyColumnName($mapping->targetEntity, $inverseJoinColumn->referencedColumnName);
}

if (empty($inverseJoinColumn->referencedColumnName)) {
$inverseJoinColumn->referencedColumnName = $namingStrategy->referenceColumnName();
}
Expand Down
12 changes: 8 additions & 4 deletions lib/Doctrine/ORM/Mapping/ToOneOwningSideMapping.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,14 @@ public static function fromMappingArrayAndName(
array|null $table,
bool $isInheritanceTypeSingleTable,
): static {
if (isset($mappingArray['joinColumns'])) {
foreach ($mappingArray['joinColumns'] as $index => $joinColumn) {
if (empty($joinColumn['name'])) {
$mappingArray['joinColumns'][$index]['name'] = $namingStrategy->joinColumnName($mappingArray['fieldName'], $name);
}
}
}

$mapping = static::fromMappingArray($mappingArray);

assert($mapping->isToOneOwningSide());
Expand All @@ -100,10 +108,6 @@ public static function fromMappingArrayAndName(
}
}

if (empty($joinColumn->name)) {
$joinColumn->name = $namingStrategy->joinColumnName($mapping->fieldName, $name);
}

if (empty($joinColumn->referencedColumnName)) {
$joinColumn->referencedColumnName = $namingStrategy->referenceColumnName();
}
Expand Down
3 changes: 1 addition & 2 deletions tests/Doctrine/Tests/ORM/Mapping/JoinColumnMappingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,8 @@ final class JoinColumnMappingTest extends TestCase
{
public function testItSurvivesSerialization(): void
{
$mapping = new JoinColumnMapping('id');
$mapping = new JoinColumnMapping('foo', 'id');

$mapping->name = 'foo';
$mapping->unique = true;
$mapping->quoted = true;
$mapping->fieldName = 'bar';
Expand Down
4 changes: 2 additions & 2 deletions tests/Doctrine/Tests/ORM/Mapping/JoinTableMappingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ public function testItSurvivesSerialization(): void
$mapping = new JoinTableMapping('bar');

$mapping->quoted = true;
$mapping->joinColumns = [new JoinColumnMapping('id')];
$mapping->inverseJoinColumns = [new JoinColumnMapping('id')];
$mapping->joinColumns = [new JoinColumnMapping('foo_id', 'id')];
$mapping->inverseJoinColumns = [new JoinColumnMapping('bar_id', 'id')];
$mapping->schema = 'foo';
$mapping->options = ['foo' => 'bar'];

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public function testItSurvivesSerialization(): void
targetEntity: self::class,
);

$mapping->joinColumns = [new JoinColumnMapping('id')];
$mapping->joinColumns = [new JoinColumnMapping('foo_id', 'id')];
$mapping->joinColumnFieldNames = ['foo' => 'bar'];
$mapping->sourceToTargetKeyColumns = ['foo' => 'bar'];
$mapping->targetToSourceKeyColumns = ['bar' => 'foo'];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public function testItSurvivesSerialization(): void
targetEntity: self::class,
);

$mapping->joinColumns = [new JoinColumnMapping('id')];
$mapping->joinColumns = [new JoinColumnMapping('foo_id', 'id')];
$mapping->joinColumnFieldNames = ['foo' => 'bar'];
$mapping->sourceToTargetKeyColumns = ['foo' => 'bar'];
$mapping->targetToSourceKeyColumns = ['bar' => 'foo'];
Expand Down

0 comments on commit ee62934

Please sign in to comment.