diff --git a/src/Command/ImportCMBBatteryDataCommand.php b/src/Command/ImportCMBBatteryDataCommand.php index 2d2f699f..09ca7a09 100644 --- a/src/Command/ImportCMBBatteryDataCommand.php +++ b/src/Command/ImportCMBBatteryDataCommand.php @@ -13,16 +13,23 @@ use App\Entity\BatteryManufacturer; use App\Entity\BatteryModel; use App\Entity\BatterySize; use App\Entity\Battery; +use App\Entity\VehicleManufacturer; +use App\Entity\Vehicle; class ImportCMBBatteryDataCommand extends Command { // field index in csv file + const F_VEHICLE_MANUFACTURER = 1; + const F_VEHICLE_MAKE = 2; + const F_VEHICLE_YEAR = 3; const F_BATT_SDFC = 4; const F_BATT_ULTRAMAX = 5; const F_BATT_MOTOLITE = 6; const F_BATT_MARATHONER = 7; const F_BATT_EXCEL = 8; + const STR_PRESENT = 'Present'; + protected $em; protected $bmanu_hash; @@ -85,6 +92,8 @@ class ImportCMBBatteryDataCommand extends Command $brand_marathoner = ''; $brand_excel = ''; + $comp_batteries = []; + while (($fields = fgetcsv($fh)) !== false) { if ($row_num < 1) @@ -93,12 +102,18 @@ class ImportCMBBatteryDataCommand extends Command continue; } + // battery info $sdfc = trim($fields[self::F_BATT_SDFC]); $ultramax = trim($fields[self::F_BATT_ULTRAMAX]); $motolite = trim($fields[self::F_BATT_MOTOLITE]); $marathoner = trim($fields[self::F_BATT_MARATHONER]); $excel = trim($fields[self::F_BATT_EXCEL]); + // vehicle info + $manufacturer = trim($fields[self::F_VEHICLE_MANUFACTURER]); + $make = trim($fields[self::F_VEHICLE_MAKE]); + $year = trim($fields[self::F_VEHICLE_YEAR]); + // get battery manufacturer when row_num == 1 if ($row_num == 1) { @@ -171,6 +186,7 @@ class ImportCMBBatteryDataCommand extends Command if (!(empty($sdfc))) { $this->addBattery($brand_sdfc, $sdfc); + $comp_batteries[] = $$this->batt_hash[$brand_sdfc][$brand_sdfc][$sdfc]; } } @@ -179,6 +195,7 @@ class ImportCMBBatteryDataCommand extends Command if (!(empty($ultramax))) { $this->addBattery($brand_ultramax, $ultramax); + $comp_batteries[] = $$this->batt_hash[$brand_ultramax][$brand_ultramax][$ultramax]; } } @@ -187,6 +204,7 @@ class ImportCMBBatteryDataCommand extends Command if (!(empty($motolite))) { $this->addBattery($brand_motolite, $motolite); + $comp_batteries[] = $this->batt_hash[$brand_motolite][$brand_motolite][$motolite]; } } @@ -195,6 +213,7 @@ class ImportCMBBatteryDataCommand extends Command if (!(empty($marathoner))) { $this->addBattery($brand_marathoner, $marathoner); + $comp_batteries[] = $this->batt_hash[$brand_marathoner][$brand_marathoner][$marathoner]; } } @@ -203,9 +222,20 @@ class ImportCMBBatteryDataCommand extends Command if (!(empty($excel))) { $this->addBattery($brand_excel, $excel); + $comp_batteriesp[] = $this->batt_hash[$brand_excel][$brand_excel][$excel]; } } + // vehicle data + // check if vehicle manufacturer has been added + if (!isset($this->vmanu_hash[$manufacturer])) + $this->addVehicleManufacturer($manufacturer); + + // check if vehicle make has been added + if (!isset($this->vmake_hash[$manufacturer][$make])) + { + $this->addVehicleMake($manufacturer, $make, $year, $comp_batteries); + } $row_num++; } @@ -289,7 +319,62 @@ class ImportCMBBatteryDataCommand extends Command $this->em->flush(); } + protected function addVehicleManufacturer($name) + { + // save to db + $vehicle_manufacturer = new VehicleManufacturer(); + $vehicle_manufacturer->setName($name); + + $this->em->persist($vehicle_manufacturer); + $this->em->flush(); + + // add to hash + $this->vmanu_hash[$name] = $vehicle_manufacturer; + } + + protected function addVehicleMake($manufacturer, $make, $year, $batteries) + { + // save to db + $vehicle = new Vehicle(); + + $vmanu = $this->vmanu_hash[$manufacturer]; + + // parse year from and year to + $year_from = ''; + $year_to = ''; + + if (!empty($year)) + { + $model_years = explode('-', $year); + $year_from = $model_years[0]; + if (!empty($year_to)) + $year_to = $model_years[1]; + + // check if $year_to is the string "Present" + // if so, set to 0, for now + if ($year_to == self::STR_PRESENT) + $year_to = 0; + } + + $vehicle->setManufacturer($vmanu) + ->setMake($make) + ->setModelYearFrom($year_from) + ->setModelYearTo($year_to); + + // add vehicle to manufacturer + $vmanu->addVehicle($vehicle); + + // add battery to vehicle + foreach ($batteries as $battery) + { + $vehicle->addBattery($battery); + } + + $this->em->persist($vmanu); + $this->em->persist($vehicle); + $this->em->flush(); + } protected function loadBatteryManufacturers() { @@ -344,12 +429,28 @@ class ImportCMBBatteryDataCommand extends Command protected function loadVehicleManufacturers() { + $this->vmanu_hash = []; + + $vmanus = $this->em->getRepository(VehicleManufacturer::class)->findAll(); + foreach ($vmanus as $vmanu) + { + $name = $vmanu->getName(); + $this->vmanu_hash[$name] = $vmanu; + } } protected function loadVehicleMakes() { + $this->vmake_hash = []; + + $vmakes = $this->em->getRepository(Vehicle::class)->findAll(); + foreach ($vmakes as $vmake) + { + $manufacturer = $vmake->getManufacturer()->getName(); + $make = $vmake->getMake(); + + $this->vmake_hash[$manufacturer][$make] = $vmake; + } } - - }