-->

Saving related objects in API Platform

2019-08-27 23:48发布

问题:

I am new to API Platform and Symfony 4, I have a situation where we have 3 entity related to each other: User, Jobseeker, JobseekerLocation so when we need add register new Jobseeker can we make entry in related table automatically? Some kind of triggering.

Or we should make 3 API call to reach and save the data?

回答1:

Use the @Groups()

https://api-platform.com/docs/core/serialization/

/**
 * @ApiResource()
 */ 
class User {
    /**
    * @Groups({"registration"})
    * @ORM\ManyToOne/OneToOne(...)
     */
    private $jobSeeker;
}


/**
 * @ApiResource()
 */ 
class JobSeeker {
    /**
    * @Groups({"registration"})
    * @ORM\ManyToOne(...)
     */
    private $jobSeekerLocation;
}

/**
 * @ApiResource()
 */ 
class JobSeekerLocation {

    /**
    * @Groups({"registration"})
    * @ORM\Column(...)
     */
    private $city;
}