Create command to generate the ids for existing customers. Create sql command to set generated ids for customers. #638
This commit is contained in:
parent
cd7b3c9e62
commit
294aafc6ec
3 changed files with 119 additions and 114 deletions
106
src/Command/CreateCustomerGeneratedIdCommand.php
Normal file
106
src/Command/CreateCustomerGeneratedIdCommand.php
Normal file
|
|
@ -0,0 +1,106 @@
|
|||
<?php
|
||||
|
||||
namespace App\Command;
|
||||
|
||||
use Symfony\Component\Console\Command\Command;
|
||||
use Symfony\Component\Console\Input\InputArgument;
|
||||
use Symfony\Component\Console\Input\InputOption;
|
||||
use Symfony\Component\Console\Input\InputInterface;
|
||||
use Symfony\Component\Console\Output\OutputInterface;
|
||||
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
use Doctrine\ORM\Query;
|
||||
use Doctrine\DBAL\DBALException;
|
||||
|
||||
use App\Entity\Customer;
|
||||
|
||||
use PDO;
|
||||
|
||||
class CreateCustomerGeneratedIdCommand extends Command
|
||||
{
|
||||
const GENERATED_ID_LENGTH = 40;
|
||||
|
||||
private $em;
|
||||
|
||||
public function __construct(EntityManagerInterface $em)
|
||||
{
|
||||
$this->em = $em;
|
||||
|
||||
parent::__construct();
|
||||
}
|
||||
|
||||
protected function configure()
|
||||
{
|
||||
$this->setName('customer:creategeneratedid')
|
||||
->setDescription('Create customer generated id and output to file.')
|
||||
->setHelp('Create customer generated id and output to file.')
|
||||
->addArgument('output_file', InputArgument::REQUIRED, 'Output filename');
|
||||
}
|
||||
|
||||
protected function execute(InputInterface $input, OutputInterface $output)
|
||||
{
|
||||
$em = $this->em;
|
||||
$db = $em->getConnection();
|
||||
|
||||
$output_file = $input->getArgument('output_file');
|
||||
|
||||
// get all customers with no generated ids. First time to run this would mean all customers
|
||||
$cust_q = $em->createQuery('SELECT c from App\Entity\Customer c where c.generated_id is null');
|
||||
|
||||
$customers = $cust_q->iterate();
|
||||
|
||||
$output_info = [];
|
||||
foreach ($customers as $row)
|
||||
{
|
||||
$cust = $row[0];
|
||||
$output->writeln('Processing customer ' . $cust->getID());
|
||||
|
||||
$generated_id = $this->generateUniqueId(self::GENERATED_ID_LENGTH);
|
||||
|
||||
// set format of csv file customer id, generated id
|
||||
$output_info[] = [
|
||||
$cust->getID(),
|
||||
$generated_id,
|
||||
];
|
||||
|
||||
$em->detach($row[0]);
|
||||
}
|
||||
|
||||
// write to output file
|
||||
$this->outputCustomerGeneratedIds($output_file, $output_info);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
protected function generateUniqueId($str_length)
|
||||
{
|
||||
$charset = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
|
||||
$rand_string = '';
|
||||
$desired_length = 10;
|
||||
|
||||
$rand_string = substr(str_shuffle($charset), 0, $desired_length);
|
||||
|
||||
$salt = time() . $rand_string;
|
||||
|
||||
return substr(md5($salt), 0, $str_length);
|
||||
}
|
||||
|
||||
protected function outputCustomerGeneratedIds($output_file, $entries)
|
||||
{
|
||||
try
|
||||
{
|
||||
$fh = fopen($output_file, "w");
|
||||
}
|
||||
catch (Exception $e)
|
||||
{
|
||||
throw new Exception('The file "' . $report_file . '" could be opened.');
|
||||
}
|
||||
|
||||
foreach($entries as $row)
|
||||
{
|
||||
fputcsv($fh, $row);
|
||||
}
|
||||
|
||||
fclose($fh);
|
||||
}
|
||||
}
|
||||
|
|
@ -1,114 +0,0 @@
|
|||
<?php
|
||||
|
||||
namespace App\Command;
|
||||
|
||||
use Symfony\Component\Console\Command\Command;
|
||||
use Symfony\Component\Console\Input\InputArgument;
|
||||
use Symfony\Component\Console\Input\InputOption;
|
||||
use Symfony\Component\Console\Input\InputInterface;
|
||||
use Symfony\Component\Console\Output\OutputInterface;
|
||||
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
use Doctrine\ORM\Query;
|
||||
use Doctrine\DBAL\DBALException;
|
||||
|
||||
use App\Entity\Customer;
|
||||
|
||||
use PDO;
|
||||
|
||||
class SetCustomerGeneratedIdCommand extends Command
|
||||
{
|
||||
const GENERATED_ID_LENGTH = 40;
|
||||
|
||||
private $em;
|
||||
protected $cust_generated_ids;
|
||||
|
||||
public function __construct(EntityManagerInterface $em)
|
||||
{
|
||||
$this->em = $em;
|
||||
|
||||
$this->loadGeneratedIds();
|
||||
|
||||
parent::__construct();
|
||||
}
|
||||
|
||||
protected function configure()
|
||||
{
|
||||
$this->setName('customer:setgeneratedid')
|
||||
->setDescription('Set customer generated id.')
|
||||
->setHelp('Set customer generated id.');
|
||||
}
|
||||
|
||||
protected function execute(InputInterface $input, OutputInterface $output)
|
||||
{
|
||||
$em = $this->em;
|
||||
$db = $em->getConnection();
|
||||
|
||||
$em->getConnection()->getConfiguration()->setSQLLogger(null);
|
||||
|
||||
// get all customers with no generated ids. First time to run this would mean all customers
|
||||
$cust_q = $em->createQuery('SELECT c from App\Entity\Customer c where c.generated_id is null');
|
||||
|
||||
$customers = $cust_q->iterate();
|
||||
foreach ($customers as $row)
|
||||
{
|
||||
$cust = $row[0];
|
||||
$output->writeln('Processing customer ' . $cust->getID());
|
||||
|
||||
while (true)
|
||||
{
|
||||
$generated_id = $this->generateUniqueId(self::GENERATED_ID_LENGTH);
|
||||
|
||||
if (!isset($this->cust_generated_ids[$generated_id]))
|
||||
{
|
||||
$update_sql = 'UPDATE customer SET generated_id = :generated_id WHERE id = :cust_id';
|
||||
$update_stmt = $db->prepare($update_sql);
|
||||
$update_stmt->execute([
|
||||
'generated_id' => $generated_id,
|
||||
'cust_id' => $cust->getID(),
|
||||
]);
|
||||
|
||||
// add generated id to hash
|
||||
$this->cust_generated_ids[$generated_id] = $cust;
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
$em->detach($row[0]);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
protected function generateUniqueId($str_length)
|
||||
{
|
||||
return substr(md5(time()), 0, $str_length);
|
||||
}
|
||||
|
||||
protected function loadGeneratedIds()
|
||||
{
|
||||
error_log('loading generated ids ');
|
||||
$cust_q = $this->em->createQuery('select c from App\Entity\Customer c where c.generated_id is not null');
|
||||
$cust_iter = $cust_q->iterate();
|
||||
|
||||
$this->cust_generated_ids = [];
|
||||
foreach ($cust_iter as $row)
|
||||
{
|
||||
$customer = $row[0];
|
||||
$generated_id = $customer->getGeneratedID();
|
||||
error_log('generated id = ' . $generated_id);
|
||||
if ($generated_id != null)
|
||||
{
|
||||
error_log('generated id is not null ' . $generated_id);
|
||||
if (!isset($this->cust_generated_ids[$generated_id]))
|
||||
{
|
||||
error_log('setting to hash ' . $generated_id);
|
||||
$this->cust_generated_ids[$generated_id] = $customer;
|
||||
}
|
||||
}
|
||||
|
||||
$this->em->detach($row[0]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,13 @@
|
|||
CREATE TABLE temp_table (id int(11), generated_id varchar(40));
|
||||
|
||||
LOAD DATA INFILE '/tmp/generated_ids.csv'
|
||||
INTO TABLE temp_table
|
||||
FIELDS TERMINATED BY ','
|
||||
ENCLOSED BY '"'
|
||||
LINES TERMINATED BY '\n'
|
||||
(id, generated_id);
|
||||
|
||||
UPDATE customer INNER JOIN temp_table ON temp_table.id = customer.id
|
||||
SET customer.generated_id = temp_table.generated_id;
|
||||
|
||||
DROP TABLE temp_table;
|
||||
Loading…
Reference in a new issue