FOSRestBundle Load Entety from Id in FormType

Hello I’m trying to create a endpoint which I can validate with a FormType and resolve Ids from Entities to the actual Entities, so I can persist them in the Database

{
    "tenderId": 54,
    "providerId": 12,
    "vehiclePriceConditions": [{
        "vehicleId": 1,
        "rebate": "0.07",
        "rebateEuro": "321",
    }]
}

The Whole Object above should be one Entity Offer and the vehiclePriceConditions one Entity called VehiclePriceConditions. With the request I would like to dynamically create the VehiclePriceConditions and the Offer and add the VehiclePriceConditions to the Offer.
As of right now im just extracting everything like this (Without FormTypes):

       public function providerSubmit(Request $request)
       {
        $data = $request->request->all();
            $tender = $this->getDoctrine()
                ->getRepository(Tender::class)
                ->find($data['tenderId']);

            $provider = $this->getDoctrine()
                ->getRepository(User::class)
                ->find($data['providerId']);

        $entityManager = $this->getDoctrine()->getManager();

        $submission = new ProviderSubmission();
        $submission->setTenderId($tender);
        $submission->setProviderId($provider);
        $submission->setStatus('submitted');
        $submission->setSubmissionDate(new DateTime());

        foreach ($data['vehiclePriceConditions'] as $condition) {
            $submission->addVehiclePriceCondition(
                $this->createPriceCondition($condition)
            );
        }

        $entityManager->persist($submission);
        $entityManager->flush();

        return new JsonResponse(['success' => 'Successful tender submission']);
    }

       public function providerSubmit(Request $request)
    {
        $data = $request->request->all();
            $tender = $this->getDoctrine()
                ->getRepository(Tender::class)
                ->find($data['tenderId']);

            $provider = $this->getDoctrine()
                ->getRepository(User::class)
                ->find($data['providerId']);

        $entityManager = $this->getDoctrine()->getManager();

        $submission = new ProviderSubmission();
        $submission->setTenderId($tender);
        $submission->setProviderId($provider);
        $submission->setStatus('submitted');
        $submission->setSubmissionDate(new DateTime());

        foreach ($data['vehiclePriceConditions'] as $condition) {
            $submission->addVehiclePriceCondition(
                $this->createPriceCondition($condition)
            );
        }

        $entityManager->persist($submission);
        $entityManager->flush();

        return new JsonResponse(['success' => 'Successful tender submission']);
    }

    public function createPriceCondition(array $condition): VehiclePriceCondition
    {
        $c = new VehiclePriceCondition();
        $vehicle = $this->getDoctrine()->getRepository(Vehicles::class)->find($condition['vehicleId']);
        $c->setVehicle($vehicle);
        $c->setRebate($condition['rebate'] ?? 0);
        $c->setRebateEuro($condition['rebateEuro'] ?? 0);

        return $c;
    }

Source: Symfony Questions

Was this helpful?

0 / 0

Leave a Reply 0

Your email address will not be published. Required fields are marked *