projectDir = $kernel->getProjectDir(); $this->fileTypeRepository = $fileTypeRepository; } protected function configure() { $this // the name of the command (the part after "bin/console") ->setName('filetype:create') // the short description shown while running "php bin/console list" ->setDescription('import file type') // add 'processDate' argument ->addArgument('process_date', InputArgument::OPTIONAL, 'Date of the process', date_create()->format('Y-m-d')); } public function execute(InputInterface $input, OutputInterface $output) { #get process date from config $processDate = $input->getArgument('process_date'); // outputs multiple lines to the console (adding "\n" at the end of each line) $output->writeln([ 'Import from csv to table file_type at DATABASE_PIM ', '============', 'upload file CSV =>', '============' ]); #convert csv file content into iterable php $fileTypes = $this->getCsvRowsAsArray(); #Loop over records foreach ($fileTypes as $fileType) { # Update if matching records found in DB$this->elementTypeRepository = $elementTypeRepository; $existingFileType = $this->fileTypeRepository->findByNameAndExtension($fileType['name'], $fileType['extension']); if ($existingFileType) { #update IF matching records found in DB $this->updateFileType($fileType, $existingFileType); continue; } else { #create new records if matching records not found in DV $this->createFileType($fileType); } } $io = new SymfonyStyle($input, $output); $io->success('File type was created to Meastro templates'); } public function getCsvRowsAsArray() { $projectDir = $this->projectDir; $inputFile = $projectDir . "/src/DataFixtures/file_type.csv"; #create decoder $decoder = new Serializer([new ObjectNormalizer()], [new CsvEncoder()]); #file_get_contents $rows = $decoder->decode(file_get_contents($inputFile), 'csv'); return $rows; } public function updateFiletype($fileType, $existingFileType) { $existingFileType->setName($fileType['name']); $existingFileType->setExtension($fileType['extension']); $this->fileTypeRepository->save($existingFileType); } public function createFileType($fileType) { $fileTypeEntity = new FileType(); $fileTypeEntity->setName($fileType['name']); $fileTypeEntity->setExtension($fileType['extension']); $this->fileTypeRepository->save($fileTypeEntity); } }