Skip to content

Issue with IRI resolution for sub-resources with fetch_data set to false #8124

Description

@leo7418

API Platform version(s) affected: 3.3.11

Description
An error occurs when attempting to retrieve a sub-resource (Employee) via its IRI, using the fetch_data option set to false. The error Doctrine\ORM\Exception\UnrecognizedIdentifierFields is raised, indicating that the required identifier fields ('companyId') are not present on the App\Entity\Employee class. This issue directly affects the use of the SearchFilter when an IRI is sent to it.

How to reproduce

  1. Use the Employee and Company entities from the API Platform documentation (https://api-platform.com/docs/core/subresources/#company-employees).
  2. Execute the provided PHP code to create instances of Company and Employee, persist them, and then retrieve an instance of Employee by its IRI with fetch_data set to false.
$entityManager = ... ;
$iriConverter = ... ;

$company = new Company();
$company->name = 'test';
$entityManager->persist($company);

for ($i = 0; $i < 3; ++$i) {
    $employee = new Employee();
    $employee->name = "Employee numer $i";
    $employee->company = $company;
    $company->employees[] = $employee;
    $entityManager->persist($employee);
}

$entityManager->flush();

$employee = $company->employees[1];
dump($employee->getId());
$employeeIri = $iriConverter->getIriFromResource($employee);
dump($employeeIri);
$employee = $iriConverter->getResourceFromIri($employeeIri, ['fetch_data' => false]); // Error here: Doctrine\ORM\Exception\UnrecognizedIdentifierFields: Unrecognized identifier fields: "companyId" are not present on class "App\Entity\Employee"
dump($employee->getId());

Additional Context
The fetch_data false option is intended to minimize data loading when retrieving entities via an IRI, typically with the SearchFilter. However, the indicated error suggests an issue in managing the identifiers of the Employee entity when accessed in this manner, leading to a malfunction in the sub-resources mechanism.

Metadata

Metadata

Assignees

No one assigned

    Labels

    StateState providers/processorsdoctrine

    Type

    Fields

    No fields configured for Bug.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions