Skip to content

Commit

Permalink
Disallow use of fetch=SUBSELECT on to-one associations.
Browse files Browse the repository at this point in the history
  • Loading branch information
beberlei committed Oct 10, 2023
1 parent 41410e6 commit ff28ba8
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 0 deletions.
4 changes: 4 additions & 0 deletions lib/Doctrine/ORM/Mapping/ClassMetadataInfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -1831,6 +1831,10 @@ protected function _validateAndCompleteAssociationMapping(array $mapping)
$mapping['fetch'] = self::FETCH_LAZY;
}

if ($mapping['fetch'] === self::FETCH_SUBSELECT && $mapping['type'] & self::TO_ONE) {
throw MappingException::illegalFetchSubselectAssociation($this->name, $mapping['fieldName']);
}

// Cascades
$cascades = isset($mapping['cascade']) ? array_map('strtolower', $mapping['cascade']) : [];

Expand Down
9 changes: 9 additions & 0 deletions lib/Doctrine/ORM/Mapping/MappingException.php
Original file line number Diff line number Diff line change
Expand Up @@ -742,6 +742,15 @@ public static function illegalToManyIdentifierAssociation($className, $field)
));
}

public static function illegalFetchSubselectAssociation(string $className, string $field): MappingException
{
return new self(sprintf(
'Cannot use fetch-mode SUBSELECT for a to-one association on entity "%s#%s".',
$className,
$field
));
}

/**
* @param string $className
*
Expand Down
14 changes: 14 additions & 0 deletions tests/Doctrine/Tests/ORM/Mapping/ClassMetadataTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1353,6 +1353,20 @@ public function testRejectsEmbeddableWithoutValidClassName(): void
'columnPrefix' => false,
]);
}

public function testIllegalFetchModeSubselect(): void
{
$metadata = new ClassMetadata(TestEntity1::class);

$this->expectException(MappingException::class);
$this->expectExceptionMessage('Cannot use fetch-mode SUBSELECT for a to-one association on entity');

$metadata->mapManyToOne([
'fieldName' => 'association',
'targetEntity' => DDC2700MappedSuperClass::class,
'fetch' => ClassMetadata::FETCH_SUBSELECT,
]);
}
}

/** @MappedSuperclass */
Expand Down

0 comments on commit ff28ba8

Please sign in to comment.