I have an entity Professionnal with Jobs in API Plateform project 3.2.
When I call host/api/professionnals
I got a result like this :
{
"data": [
{
"id": "/api/professionnals/1",
"type": "Professionnal",
"attributes": {
"email": "[email protected]",
"firstname": "JOHN",
"lastname": "DOE"
},
"relationships": {
"jobs": {
"data": [
{
"type": "Job",
"id": "/api/jobs/1"
},
{
"type": "Job",
"id": "/api/jobs/2"
}
]
}
}
},
...
]
}
I would like the job label property but it seems to be more difficult with relation ManyToMany in API Plateform to get it.
Professionnal.php Entity
<?php
namespace AppEntity;
use ...
#[ApiResource(normalizationContext: ['groups' => ['professionnal:read']])]
#[ORMEntity(repositoryClass: ProfessionnalRepository::class)]
#[ORMUniqueConstraint(name: 'UNIQ_IDENTIFIER_EMAIL', fields: ['email'])]
class Professionnal implements UserInterface, PasswordAuthenticatedUserInterface {
...
#[AssertNotBlank]
#[AssertEmail]
#[ORMColumn(length: 180, unique: true)]
#[Groups(['professionnal:read'])]
private ?string $email = null;
...
#[ORMColumn(length: 150)]
#[Groups(['professionnal:read'])]
private ?string $firstname = null;
#[ORMColumn(length: 255)]
#[Groups(['professionnal:read'])]
private ?string $lastname = null;
...
/**
* @var Collection<int, Job>
*/
#[ORMManyToMany(targetEntity: Job::class, inversedBy: 'professionnals', fetch: "EAGER")]
#[ORMJoinTable(name: 'professionnal_job')]
#[ORMJoinColumn(name: 'professionnal_id', referencedColumnName: 'id', nullable: false)]
#[ORMInverseJoinColumn(name: 'job_id', referencedColumnName: 'id', nullable: false)]
#[Groups(['professionnal:read'])]
private Collection $jobs;
public function __construct() {
$this->jobs = new ArrayCollection();
}
...
/**
* @return Job[]
*/
#[Groups(['professionnal:read'])]
public function getJobs(): array {
return $this->jobs->getValues();
}
}
Job.php Entity
<?php
namespace AppEntity;
...
#[ORMEntity(repositoryClass: JobRepository::class)]
#[ApiResource()]
class Job {
#[ORMId]
#[ORMGeneratedValue]
#[ORMColumn]
private ?int $id = null;
#[ORMColumn(length: 255)]
#[Groups(['professionnal:read', 'job:read'])]
private ?string $label = null;
/**
* @var Collection<int, Professionnal>
*/
#[ORMManyToMany(targetEntity: Professionnal::class, mappedBy: 'jobs')]
private Collection $professionnals;
public function __construct() {
$this->professionnals = new ArrayCollection();
}
public function __toString(): string {
return (is_null($this->getLabel())) ? '' : $this->getLabel();
}
public function getId(): ?int {
return $this->id;
}
public function getLabel(): ?string {
return $this->label;
}
public function setLabel(string $label): static {
$this->label = $label;
return $this;
}
/**
* @return Collection<int, Professionnal>
*/
public function getProfessionnals(): Collection {
return $this->professionnals;
}
...
}
I tried to add this config code without success :/
api_platform:
eager_loading:
max_joins: 100
Someone has got an idea ? Should I find a workaround or there is a proper solution to do this ?