Skip to content
Open
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
14 changes: 11 additions & 3 deletions src/Doctrine/Orm/State/ItemProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,17 @@ public function provide(Operation $operation, array $uriVariables = [], array $c
}

$fetchData = $context['fetch_data'] ?? true;
if (!$fetchData && \array_key_exists('id', $uriVariables)) {
// todo : if uriVariables don't contain the id, this fails. This should behave like it does in the following code
return $manager->getReference($entityClass, $uriVariables);
if (!$fetchData) {
$identifierFields = $manager->getClassMetadata($entityClass)->getIdentifierFieldNames();
$identifiers = array_intersect_key($uriVariables, array_flip($identifierFields));

// Subresources also carry the parent link (e.g. "companyId"), which is not an identifier of
// the entity and would make getReference() throw UnrecognizedIdentifierFields. Only build a
// reference when every own identifier is present; otherwise fall through to a query that
// resolves the link.
if (\count($identifiers) === \count($identifierFields)) {
return $manager->getReference($entityClass, $identifiers);
}
}

$repository = $manager->getRepository($entityClass);
Expand Down
30 changes: 30 additions & 0 deletions src/Doctrine/Orm/Tests/State/ItemProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,36 @@ public function testGetItemDoubleIdentifier(): void
$this->assertEquals($returnObject, $dataProvider->provide($operation, ['ida' => 1, 'idb' => 2], $context));
}

public function testGetItemWithFetchDataFalseOnSubresourceFiltersParentLink(): void
{
$reference = new Employee();

$classMetadataMock = $this->createMock(ClassMetadata::class);
$classMetadataMock->method('getIdentifierFieldNames')->willReturn(['id']);

$managerMock = $this->createMock(EntityManagerInterface::class);
$managerMock->method('getClassMetadata')->with(Employee::class)->willReturn($classMetadataMock);
$managerMock->expects($this->once())
->method('getReference')
->with(Employee::class, ['id' => 2])
->willReturn($reference);

$managerRegistryMock = $this->createMock(ManagerRegistry::class);
$managerRegistryMock->method('getManagerForClass')->with(Employee::class)->willReturn($managerMock);

$operation = (new Get())->withUriVariables([
'companyId' => (new Link())->withFromClass(Company::class)->withToProperty('company'),
'id' => (new Link())->withFromClass(Employee::class)->withIdentifiers(['id']),
])->withName('get')->withClass(Employee::class);

$dataProvider = new ItemProvider(
$this->createStub(ResourceMetadataCollectionFactoryInterface::class),
$managerRegistryMock,
);

$this->assertSame($reference, $dataProvider->provide($operation, ['companyId' => 1, 'id' => 2], ['fetch_data' => false]));
}

public function testQueryResultExtension(): void
{
$returnObject = new \stdClass();
Expand Down
Loading