Add command to add battery manufacturers from given csv file. #270
This commit is contained in:
parent
f205f04b48
commit
5980715128
1 changed files with 90 additions and 0 deletions
90
src/Command/ImportBatteryManufacturersCommand.php
Normal file
90
src/Command/ImportBatteryManufacturersCommand.php
Normal file
|
|
@ -0,0 +1,90 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Command;
|
||||||
|
|
||||||
|
use Symfony\Component\Console\Command\Command;
|
||||||
|
use Symfony\Component\Console\Input\InputArgument;
|
||||||
|
use Symfony\Component\Console\Input\InputInterface;
|
||||||
|
use Symfony\Component\Console\Output\OutputInterface;
|
||||||
|
|
||||||
|
use Doctrine\Common\Persistence\ObjectManager;
|
||||||
|
|
||||||
|
use App\Entity\BatteryManufacturer;
|
||||||
|
|
||||||
|
class ImportBatteryManufacturersCommand extends Command
|
||||||
|
{
|
||||||
|
// field index in csv file
|
||||||
|
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;
|
||||||
|
|
||||||
|
protected $em;
|
||||||
|
|
||||||
|
public function __construct(ObjectManager $om)
|
||||||
|
{
|
||||||
|
$this->em = $om;
|
||||||
|
|
||||||
|
parent::__construct();
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function configure()
|
||||||
|
{
|
||||||
|
$this->setName('batterymanufacturer:import')
|
||||||
|
->setDescription('Retrieve from a CSV file battery manufacturer information.')
|
||||||
|
->setHelp('Creates battery manufacturers based on data from imported CSV.')
|
||||||
|
->addArgument('file', InputArgument::REQUIRED, 'Path to the CSV file.');
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function execute(InputInterface $input, OutputInterface $output)
|
||||||
|
{
|
||||||
|
$csv_file = $input->getArgument('file');
|
||||||
|
|
||||||
|
// attempt to open file
|
||||||
|
try
|
||||||
|
{
|
||||||
|
$fh = fopen($csv_file, "r");
|
||||||
|
}
|
||||||
|
catch (Exception $e)
|
||||||
|
{
|
||||||
|
throw new Exception('The file "' . $csv_file . '" could be read.');
|
||||||
|
}
|
||||||
|
|
||||||
|
// get entity manager
|
||||||
|
$em = $this->em;
|
||||||
|
|
||||||
|
// find the battery manufacturer row = 2nd row
|
||||||
|
$row_num = 0;
|
||||||
|
while (($fields = fgetcsv($fh)) !== false)
|
||||||
|
{
|
||||||
|
if ($row_num < 1)
|
||||||
|
{
|
||||||
|
$row_num++;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// get battery manufacturer when row_num == 1
|
||||||
|
if ($row_num == 1)
|
||||||
|
{
|
||||||
|
$this->addBatteryManufacturer(trim($fields[self::F_BATT_SDFC]));
|
||||||
|
$this->addBatteryManufacturer(trim($fields[self::F_BATT_ULTRAMAX]));
|
||||||
|
$this->addBatteryManufacturer(trim($fields[self::F_BATT_MOTOLITE]));
|
||||||
|
$this->addBatteryManufacturer(trim($fields[self::F_BATT_MARATHONER]));
|
||||||
|
$this->addBatteryManufacturer(trim($fields[self::F_BATT_EXCEL]));
|
||||||
|
}
|
||||||
|
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function addBatteryManufacturer($name)
|
||||||
|
{
|
||||||
|
$batt_manufacturer = new BatteryManufacturer();
|
||||||
|
|
||||||
|
$batt_manufacturer->setName($name);
|
||||||
|
|
||||||
|
$this->em->persist($batt_manufacturer);
|
||||||
|
$this->em->flush();
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in a new issue