Skip to content

Instantly share code, notes, and snippets.

@zajca
Created October 31, 2014 07:58
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save zajca/7678f599996eb76efbfd to your computer and use it in GitHub Desktop.
Save zajca/7678f599996eb76efbfd to your computer and use it in GitHub Desktop.
Symfony2 custom command for import CSV
<?php
namespace PRIA\QuizApiBundle\Command;
use PRIA\Bundle\UserBundle\EntityRepository\RoundsRepository;
use PRIA\QuizApiBundle\Entity\Answers;
use PRIA\QuizApiBundle\Entity\Questions;
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Finder\Finder;
class CSVImportCommand extends ContainerAwareCommand
{
// change these options about the file to read
private $cvsParsingOptions = array(
'finder_in' => __DIR__,
'finder_name' => 'ahmad_q.csv',
'ignoreFirstLine' => true
);
protected function configure()
{
$this->setName('QuizApi:Import')
->setDescription('Import csv file')
->addArgument(
'startDate',
InputArgument::REQUIRED,
'Date start'
);
}
protected function execute(InputInterface $input, OutputInterface $output)
{
// use the parseCSV() function
$csv = $this->parseCSV();
$startDate = new \DateTime($input->getArgument('startDate'));
$em = $this->getContainer()->get('doctrine')->getManager();
/** @var RoundsRepository $rounds */
$rounds = $em->getRepository("PRIAUserBundle:Rounds");
$round = $rounds->findOneByName("test");
foreach ($csv as $line) {
$startDate->format('Y-m-d');
$question = new Questions();
$question->setIdRound($round);
$question->setText($line[0]);
$question->setLink($line[4]);
$question->setDate(clone $startDate);
$answer = new Answers();
$answer->setIdQuestion($question);
$answer->setText($line[1]);
$answer->setRightAnswer(1);
$answer2 = new Answers();
$answer2->setIdQuestion($question);
$answer2->setText($line[2]);
$answer2->setRightAnswer(2);
$answer3 = new Answers();
$answer3->setIdQuestion($question);
$answer3->setText($line[3]);
$answer3->setRightAnswer(3);
$em->persist($answer);
$em->persist($answer2);
$em->persist($answer3);
$em->persist($question);
$startDate->modify('+1 day');
}
$em->flush();
}
/**
* Parse a csv file
*
* @return array
* @throws \Exception
*
*/
private function parseCSV()
{
$ignoreFirstLine = $this->cvsParsingOptions['ignoreFirstLine'];
$finder = new Finder();
$finder->files()
->in($this->cvsParsingOptions['finder_in'])
->name($this->cvsParsingOptions['finder_name'])
->files();
foreach ($finder as $file) {
$csv = $file;
}
if(empty($csv)){
throw new \Exception("NO CSV FILE");
}
$rows = array();
if (($handle = fopen($csv->getRealPath(), "r")) !== FALSE) {
$i = 0;
while (($data = fgetcsv($handle, null, ",")) !== FALSE) {
$i++;
if ($ignoreFirstLine && $i == 1) {
continue;
}
$rows[] = $data;
}
fclose($handle);
}
return $rows;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment