From ac908e9bcd6106b8d6c69ecb363b1f013080fccc Mon Sep 17 00:00:00 2001 From: Korina Cordero Date: Mon, 3 Feb 2020 11:23:57 +0000 Subject: [PATCH] Add saving of battery to vehicle. #323 --- src/Controller/VehicleController.php | 35 ++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/src/Controller/VehicleController.php b/src/Controller/VehicleController.php index f7f8c625..d09d3752 100644 --- a/src/Controller/VehicleController.php +++ b/src/Controller/VehicleController.php @@ -310,6 +310,41 @@ class VehicleController extends Controller } } } + // need to check if a battery has been added + if (count($current_batteries) < count($batteries)) + { + // get the ids of current batteries + $cbatt_ids = []; + foreach ($current_batteries as $cbatt) + { + $cbatt_ids = [ + $cbatt->getID(), + ]; + } + + // find the battery to add + foreach ($batteries as $batt) + { + $batt_id = $batt; + if (in_array($batt_id, $cbatt_ids)) + { + // do nothing since battery already there + continue; + } + else + { + // batt is the new battery + $battery = $em->getRepository(Battery::class)->find($batt_id); + + if (!empty($battery)) + { + $battery->addVehicle($row); + + $em->persist($battery); + } + } + } + } } else {