Skip to content

Commit

Permalink
[doctrineGh-8589] A new approach to non-nullable typed associations f…
Browse files Browse the repository at this point in the history
…or BC
  • Loading branch information
beberlei committed May 9, 2021
1 parent dacdcf2 commit 1add3d8
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 33 deletions.
21 changes: 8 additions & 13 deletions lib/Doctrine/ORM/Mapping/ClassMetadataInfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -1515,18 +1515,18 @@ private function validateAndCompleteTypedAssociationMapping(array $mapping): arr
{
$type = $this->reflClass->getProperty($mapping['fieldName'])->getType();

if (
! isset($mapping['targetEntity'])
&& ($mapping['type'] & self::TO_ONE) > 0
&& $type instanceof ReflectionNamedType
) {
if ($type === null || ($mapping['type'] & self::TO_ONE) === 0) {
return $mapping;
}

if (! isset($mapping['targetEntity']) && $type instanceof ReflectionNamedType) {
$mapping['targetEntity'] = $type->getName();
}

if ($type !== null && isset($mapping['joinColumns'])) {
if (isset($mapping['joinColumns'])) {
foreach ($mapping['joinColumns'] as &$joinColumn) {
if (! isset($joinColumn['nullable'])) {
$joinColumn['nullable'] = $type->allowsNull();
if ($type->allowsNull() === false) {
$joinColumn['nullable'] = false;
}
}
}
Expand Down Expand Up @@ -1801,7 +1801,6 @@ protected function _validateAndCompleteOneToOneMapping(array $mapping)
[
'name' => $this->namingStrategy->joinColumnName($mapping['fieldName'], $this->name),
'referencedColumnName' => $this->namingStrategy->referenceColumnName(),
'nullable' => true,
],
];
}
Expand All @@ -1819,10 +1818,6 @@ protected function _validateAndCompleteOneToOneMapping(array $mapping)
}
}

if (! isset($joinColumn['nullable'])) {
$joinColumn['nullable'] = true;
}

if (empty($joinColumn['name'])) {
$joinColumn['name'] = $this->namingStrategy->joinColumnName($mapping['fieldName'], $this->name);
}
Expand Down
6 changes: 3 additions & 3 deletions lib/Doctrine/ORM/Mapping/JoinColumn.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ final class JoinColumn implements Annotation
/** @var bool */
public $unique = false;

/** @var bool|null */
public $nullable;
/** @var bool */
public $nullable = true;

/** @var mixed */
public $onDelete;
Expand All @@ -60,7 +60,7 @@ public function __construct(
?string $name = null,
string $referencedColumnName = 'id',
bool $unique = false,
?bool $nullable = null,
bool $nullable = true,
$onDelete = null,
?string $columnDefinition = null,
?string $fieldName = null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,6 @@ class CascadeRemoveOrderEntityG
* targetEntity="Doctrine\Tests\ORM\Functional\CascadeRemoveOrderEntityO",
* inversedBy="oneToMany"
* )
* @JoinColumn(nullable=true, onDelete="SET NULL")
*/
private $ownerO;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -278,13 +278,6 @@ public function testFieldIsNullableByType(): void

// Explicit Not Nullable
$this->assertFalse($class->isNullable('username'));

// Join table Nullable
$this->assertFalse($class->getAssociationMapping('email')['joinColumns'][0]['nullable']);
$this->assertEquals(CmsEmail::class, $class->getAssociationMapping('email')['targetEntity']);

$this->assertTrue($class->getAssociationMapping('mainEmail')['joinColumns'][0]['nullable']);
$this->assertEquals(CmsEmail::class, $class->getAssociationMapping('mainEmail')['targetEntity']);
}

public function testFieldTypeFromReflection(): void
Expand Down
9 changes: 0 additions & 9 deletions tests/Doctrine/Tests/ORM/Mapping/ClassMetadataTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,15 +127,6 @@ public function testFieldIsNullableByType(): void
// Explicit Not Nullable
$cm->mapField(['fieldName' => 'username', 'length' => 50]);
$this->assertFalse($cm->isNullable('username'));

// Join table Nullable
$cm->mapOneToOne(['fieldName' => 'email', 'joinColumns' => [[]]]);
$this->assertFalse($cm->getAssociationMapping('email')['joinColumns'][0]['nullable']);
$this->assertEquals(CmsEmail::class, $cm->getAssociationMapping('email')['targetEntity']);

$cm->mapManyToOne(['fieldName' => 'mainEmail']);
$this->assertTrue($cm->getAssociationMapping('mainEmail')['joinColumns'][0]['nullable']);
$this->assertEquals(CmsEmail::class, $cm->getAssociationMapping('mainEmail')['targetEntity']);
}

public function testFieldTypeFromReflection(): void
Expand Down

0 comments on commit 1add3d8

Please sign in to comment.