Example #1
0
 /**
  * {@inheritdoc}
  */
 protected function execute(InputInterface $input, OutputInterface $output)
 {
     $c = $this->getContainer()->get('doctrine.dbal.default_connection');
     $table = $input->getOption('table');
     switch ($table) {
         case 'elects':
             $query = 'SELECT t.id, t.first_name, t.last_name, t.slug FROM ' . $table . ' AS t';
             $getSlugBase = function ($data) {
                 return $data['first_name'] . ' ' . $data['last_name'];
             };
             break;
         default:
             $query = 'SELECT t.id, t.name, t.slug FROM ' . $table . ' AS t';
             $getSlugBase = function ($data) {
                 return $data['name'];
             };
             break;
     }
     $sql = $c->prepare($query);
     $sql->execute();
     $datas = $sql->fetchAll();
     $nbRows = count($datas);
     $pg = new ProgressBar($output, $nbRows);
     $pg->setFormat('very_verbose');
     $start = true;
     $i = 1;
     foreach ($datas as $data) {
         if ($start) {
             $query = 'START TRANSACTION;';
             $start = false;
         }
         $query .= 'UPDATE ' . $table . " SET slug = '" . Slugger::uniqueSlugify($getSlugBase($data)) . "' WHERE id = " . $data['id'] . ';';
         if ($i % 1000 === 0 || $i == $nbRows) {
             $query .= 'COMMIT;';
             $start = true;
             $sql = $c->prepare($query);
             $sql->execute();
         }
         $pg->advance();
         ++$i;
     }
 }
Example #2
0
 /**
  * Generate unique slug
  *
  * @param  string  $text
  * @return string
  */
 function uniqueSlugify($text)
 {
     return Slugger::uniqueSlugify($text);
 }