Skip to content

Instantly share code, notes, and snippets.

@permatis
Last active December 19, 2015 02:24
Show Gist options
  • Save permatis/87bbd1e29fe611e0b5f7 to your computer and use it in GitHub Desktop.
Save permatis/87bbd1e29fe611e0b5f7 to your computer and use it in GitHub Desktop.
This file for make records to part. For example, i create seeder by laravel.
<?php
use App\{{ table }};
use Illuminate\Database\Seeder;
class {{ table }}Part{{ key }}Seeder extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
$array = '{{ array }}';
foreach(json_decode($array) as $val) { {{ table }}::create( (array) $val ); }
}
}
<?php
Class MakeRecordsToPart
{
protected $mysqli;
protected $table;
protected $next;
protected $page;
protected $limit = 1000;
public function __construct($host, $username, $password, $database, $table)
{
$this->mysqli = new mysqli($host, $username, $password, $database);
if ($this->mysqli->connect_errno) {
printf("Connect failed: %s\n", $this->mysqli->connect_error);
exit();
}
$this->table = $table;
}
protected function alltable()
{
return $this->mysqli->query("SELECT * FROM ".$this->table);
}
public function total()
{
return mysqli_num_rows($this->alltable());
}
public function getLimit()
{
return $this->limit;
}
public function setLimit($limit)
{
$this->limit = $limit;
}
public function getPage()
{
return $this->page;
}
public function setPage($page)
{
$this->page = $page;
}
public function column($column = '')
{
foreach ($this->alltable()->fetch_fields() as $k) {
$column[] = $k->name;
}
return (is_array($column) ? implode(', ', $column) : $column);
}
protected function queryLimit()
{
if($this->page || $this->page == '0') {
$pages = (strpos($this->page, '-')) ?
explode('-', $this->page)[1] - (explode('-', $this->page)[0]): $this->page;
$this->next = (strpos($this->page, '-')) ? explode('-', $this->page)[0] : 0;
for ($i=1; $i <= $pages ; $i++) {
$query[$i] = "SELECT ".$this->column()." FROM ".$this->table." LIMIT ".($this->next + $i - 1 ) * $this->limit.", ".$this->limit;
}
} else {
for ($i=1; $i <= floor($this->total()/$this->limit)+1; $i++) {
$query[$i] = ($i == 1) ? "SELECT ".$this->column()." FROM ".$this->table." LIMIT 0, ".$this->limit :
"SELECT ".$this->column()." FROM ".$this->table." LIMIT ".($i-1)*$this->limit.", ".$this->limit;
}
}
return $query;
}
protected function toJson($query)
{
$data = [];
while($row = $query->fetch_array(MYSQLI_ASSOC))
{
$data[] = json_encode($row);
}
$datas = '['.implode(',', $data).']';
return $datas;
}
protected function results()
{
for ($i=1; $i <= count($this->queryLimit()) ; $i++) {
$query = $this->mysqli->query($this->queryLimit()[$i]);
$json[$i] = $this->toJson($query);
}
return $json;
}
public function makeFile($fileName = '', $str = [])
{
$json = $this->results();
$fileName = ($fileName) ? $fileName : 'ExampleSeeder.stub';
$str = ($str) ? $str :['{{ array }}', '{{ table }}', '{{ key }}'];
foreach ($json as $key => $value) {
if($value != '[]') { $data[$key] = [$value, ucfirst($this->table), (($this->next) ? $this->next+$key-1 : $key)]; }
}
if($this->checkIfExists($fileName, $str)) {
foreach ($data as $key => $value) {
$file = file_get_contents($fileName);
$key = ($this->next) ? $this->next+$key-1 : $key;
if(!file_exists('seeder/'.ucfirst($this->table))) { mkdir('seeder/'.ucfirst($this->table) ,0755, true);}
file_put_contents("seeder/".ucfirst($this->table)."/".ucfirst($this->table)."Part".$key."Seeder.php", $this->toReplace($str, $value, $file));
}
}else {
echo "Sorry, file doesn't exists!";
}
}
protected function checkIfExists($fileName, $str)
{
foreach ($str as $s) {
return (strpos(file_get_contents($fileName), $s) !== false) ? true : false;
}
}
protected function toReplace($search, $replace, $file)
{
return strtr($file, array_combine($search, $replace));
}
}
// How to use.
$file = new MakeRecordsToPart('HOSTNAME', 'USERNAME', 'PASSWORD','DATABASE', 'TABLE');
echo "Total record : ".$file->total()."\n";
$file->setLimit(10);
$file->setPage('1-5'); // or $file->setPage('0')
$file->makeFile();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment