0
votes

I have an entity ServiceLevel in my Symfony 3.4 application.

 /**
 * @ORM\Table()
 * @ORM\Entity()
 */
 class ServiceLevel
 {
...

/**
 * @var FlightService
 *
 * @ORM\ManyToOne(targetEntity="PartnershipBundle\Entity\ServiceLevel\FlightService")
 * @ORM\JoinColumn(referencedColumnName="id", nullable=false)
 *
 * @Assert\NotBlank()
 *
 * @Groups({"service_level", "service_level_create"})
 */
protected $flightService;

/**
 * @var HotelService|null
 *
 * @ORM\ManyToOne(targetEntity="PartnershipBundle\Entity\ServiceLevel\HotelService")
 * @ORM\JoinColumn(referencedColumnName="id", nullable=false)
 *
 * @Assert\NotBlank()
 *
 * @Groups({"service_level", "service_level_create"})
 */
protected $hotelService;

/**
 * @var \Doctrine\Common\Collections\Collection|ExtraService[]
 *
 * @ORM\ManyToMany(targetEntity="PartnershipBundle\Entity\ServiceLevel\ExtraService", cascade={"persist"})
 * @ORM\JoinTable(name="service_level_extra_service")
 *
 * @Groups({"service_level", "service_level_create"})
 */
protected $extraService;

also, I use post request to create new ServiceLevel { "name_en": "string", "name_fr": "string", "description_en": "string", "description_fr": "string", "flight_chargeable": true, "hotel_chargeable": true, "flight_service": 1, "hotel_service": 1, "extra_service": [1,2,3] }

also, I have controller action with serializer using

$serializer = $this->get('serializer');
$validator = $this->get('validator');
$em = $this->getDoctrine()->getManager();
$data = $request->request->all();
$serviceLevel = new ServiceLevel();

$serializer->denormalize($data, ServiceLevel::class, 'array', [
   'object_to_populate' => $serviceLevel,
]);

Error I get is "message": "Expected argument of type \"PartnershipBundle\\Entity\\ServiceLevel\\FlightService or null\", \"integer\" given",

I understand why it happens, but I can't find a solution to transform integer to Entity using serializer. Is there a way to handle ManyToOne and ManyToMany relationships? Btw I don't want to create new entities, just link to existing ones.

1

1 Answers

0
votes

I think you must had @Groups({"service_level"}) to your PartnershipBundle\Entity\ServiceLevel\FlightService.

bye.