Add dealer entity and PHP script to add dealers. #580
This commit is contained in:
parent
da6b344bef
commit
8648f6f3f1
2 changed files with 187 additions and 0 deletions
81
src/Entity/Dealer.php
Normal file
81
src/Entity/Dealer.php
Normal file
|
|
@ -0,0 +1,81 @@
|
|||
<?php
|
||||
|
||||
namespace App\Entity;
|
||||
|
||||
use Doctrine\ORM\Mapping as ORM;
|
||||
|
||||
use Symfony\Component\Validator\Constraints as Assert;
|
||||
|
||||
/**
|
||||
* @ORM\Entity
|
||||
* @ORM\Table(name="dealer")
|
||||
*/
|
||||
class Dealer
|
||||
{
|
||||
// unique id
|
||||
/**
|
||||
* @ORM\Id
|
||||
* @ORM\Column(type="integer")
|
||||
* @ORM\GeneratedValue(strategy="AUTO")
|
||||
*/
|
||||
protected $id;
|
||||
|
||||
// name of dealer
|
||||
/**
|
||||
* @ORM\Column(type="string", length=80)
|
||||
* @Assert\NotBlank()
|
||||
*/
|
||||
protected $name;
|
||||
|
||||
// address
|
||||
/**
|
||||
* @ORM\Column(type="text")
|
||||
* @Assert\NotBlank()
|
||||
*/
|
||||
protected $address;
|
||||
|
||||
// branch code of dealer
|
||||
/**
|
||||
* @ORM\Column(type="string", length=80)
|
||||
* @Assert\NotBlank()
|
||||
*/
|
||||
protected $branch_code;
|
||||
|
||||
public function getId()
|
||||
{
|
||||
return $this->id;
|
||||
}
|
||||
|
||||
public function setName($name)
|
||||
{
|
||||
$this->name = $name;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getName()
|
||||
{
|
||||
return $this->name;
|
||||
}
|
||||
|
||||
public function setAddress($address)
|
||||
{
|
||||
$this->address = $address;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getAddress()
|
||||
{
|
||||
return $this->address;
|
||||
}
|
||||
|
||||
public function setBranchCode($branch_code)
|
||||
{
|
||||
$this->branch_code = $branch_code;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getBranchCode()
|
||||
{
|
||||
return $this->branch_code;
|
||||
}
|
||||
}
|
||||
106
utils/load_dealers/load_dealers.php
Normal file
106
utils/load_dealers/load_dealers.php
Normal file
|
|
@ -0,0 +1,106 @@
|
|||
<?php
|
||||
|
||||
require_once(__DIR__ . '/../../vendor/autoload.php');
|
||||
|
||||
use Symfony\Component\Dotenv\Dotenv;
|
||||
|
||||
// get database information from .env
|
||||
function getDatabaseInfo()
|
||||
{
|
||||
$dotenv = new Dotenv();
|
||||
$dotenv->loadEnv(__DIR__.'/../../.env');
|
||||
|
||||
$db_info = $_ENV['DATABASE_URL'];
|
||||
|
||||
// sample format of db_info: mysql://db_user:db_password@127.0.0.1:3306/resq?charset=utf8
|
||||
// dsn for PDO needs to be: mysql:host=127.0.0.1:3306;dbname=resq;charset=UTF8
|
||||
|
||||
preg_match('/^mysql:\/\/(.*):(.*)@(.*):(.*)\/(.*)\?(.*)/', $db_info, $result);
|
||||
|
||||
$db_type = 'mysql:host=';
|
||||
$user = $result[1];
|
||||
$pass = $result[2];
|
||||
$ip_port = $result[3] . ':' . $result[4] . ';';
|
||||
$db_name = 'dbname=' . $result[5] . ';';
|
||||
$charset = $result[6];
|
||||
|
||||
$dsn = $db_type . $ip_port . $db_name . $charset;
|
||||
|
||||
$db_data = array($dsn, $user, $pass);
|
||||
|
||||
return $db_data;
|
||||
}
|
||||
|
||||
// load csv
|
||||
$csv = fopen($argv[1], 'r');
|
||||
$output_file = $argv[2];
|
||||
|
||||
$output_fh = fopen($output_file, "w");
|
||||
|
||||
if (!file_exists($argv[1]))
|
||||
{
|
||||
$err_message = "No csv input file found." . "\n";
|
||||
fwrite($output_fh, $err_message);
|
||||
fclose($output_fh);
|
||||
exit();
|
||||
}
|
||||
|
||||
list($dsn, $user, $pass) = getDatabaseInfo();
|
||||
|
||||
// error_log($dsn);
|
||||
// error_log($user);
|
||||
// error_log($pass);
|
||||
|
||||
// connect to db
|
||||
$db = new PDO($dsn, $user, $pass);
|
||||
|
||||
// prepared statement
|
||||
$sth = $db->prepare('insert into dealer (name, address, branch_code) values (:name, :address, :branch_code)');
|
||||
|
||||
$rownum = 0;
|
||||
while (($row = fgetcsv($csv)) !== false)
|
||||
{
|
||||
// dealer csv file has a header
|
||||
if ($rownum < 1)
|
||||
{
|
||||
// skip header
|
||||
$rownum++;
|
||||
continue;
|
||||
}
|
||||
|
||||
// sample of line in csv file
|
||||
// columns are name, address, branch code
|
||||
// BATPARTS Marcos Branch,"Km. 16 Marcos Hi-way corner Mahogany Street, Santolan, Pasig",ZN03576282
|
||||
$dealer_name = trim(strtoupper($row[0]));
|
||||
$dealer_address = trim(strtoupper($row[1]));
|
||||
$dealer_branch_code = trim(strtoupper($row[2]));
|
||||
|
||||
// error_log('name ' . $dealer_name . ' address ' . $dealer_address . ' branch code ' . $dealer_branch_code);
|
||||
error_log('Processing ' . $dealer_name);
|
||||
|
||||
$res = $sth->execute([
|
||||
':name' => $dealer_name,
|
||||
':address' => $dealer_address,
|
||||
':branch_code' => $dealer_branch_code,
|
||||
]);
|
||||
|
||||
if (!$res)
|
||||
{
|
||||
// log error
|
||||
$err = $sth->errorInfo();
|
||||
$log_message = "$dealer_name - ERROR - " . $err[2] . "\n";
|
||||
}
|
||||
else
|
||||
{
|
||||
// log successful adding of dealer
|
||||
$log_message = "$dealer_name - SUCCESS - " . "\n";
|
||||
}
|
||||
fwrite($output_fh, $log_message);
|
||||
|
||||
$rownum++;
|
||||
}
|
||||
|
||||
// close file
|
||||
fclose($csv);
|
||||
fclose($output_fh);
|
||||
?>
|
||||
Loading…
Reference in a new issue