diff --git a/src/DbExporter/DbExporter.php b/src/DbExporter/DbExporter.php index 2fc74ad..2b53482 100644 --- a/src/DbExporter/DbExporter.php +++ b/src/DbExporter/DbExporter.php @@ -152,5 +152,5 @@ abstract public function convert($database = null); * * @return mixed */ - abstract protected function compile(); + abstract protected function compile($table); } diff --git a/src/DbExporter/DbMigrations.php b/src/DbExporter/DbMigrations.php index cf05a4d..4dd4570 100644 --- a/src/DbExporter/DbMigrations.php +++ b/src/DbExporter/DbMigrations.php @@ -224,7 +224,7 @@ public function columnType($type, $columns = 'columns', $method = '') * * @return string */ - protected function compile() + protected function compile($null = null) { $upSchema = ''; $downSchema = ''; diff --git a/src/DbExporter/DbSeeding.php b/src/DbExporter/DbSeeding.php index b3970a6..3a63ba8 100644 --- a/src/DbExporter/DbSeeding.php +++ b/src/DbExporter/DbSeeding.php @@ -52,11 +52,14 @@ public function write() $this->convert(); } - $seed = $this->compile(); - $absolutePath = Config::get('db-exporter.export_path.seeds'); - $this->filename = ucfirst(Str::camel($this->database)) . 'DatabaseSeeder'; - $this->makePath($absolutePath); - file_put_contents($absolutePath . "/{$this->filename}.php", $seed); + foreach ($this->seedingStub as $table => $value) + { + $seed = $this->compile($table); + $absolutePath = Config::get('db-exporter.export_path.seeds'); + $this->filename = ucfirst(Str::camel($table)) . 'DatabaseSeeder'; + $this->makePath($absolutePath); + file_put_contents($absolutePath . "/{$this->filename}.php", $seed); + } } //end write() @@ -77,6 +80,7 @@ public function convert($database = null) // Get the tables for the database $tables = $this->getTables(); + $result = []; // Get tables to ignore $config = config('db-exporter.seeds'); @@ -85,7 +89,6 @@ public function convert($database = null) $ignore_tables = collect($config['ignore_tables']); } - $stub = ''; // Loop over the tables foreach ($tables as $key => $value) { @@ -116,15 +119,24 @@ public function convert($database = null) } } - if ($this->hasTableData($tableData)) { - $stub .= " - DB::table('" . $tableName . "')->insert([ + $insertStub = " + \$data = [ {$insertStub} - ]);"; + ];"; + + if ($this->hasTableData($tableData)) { + $stub = $insertStub.' + + foreach($data as $item) + { + $this->saveData("'.$tableName.'", $item); + }'; } + + $result[$tableName] = $stub; }//end foreach - $this->seedingStub = $stub; + $this->seedingStub = $result; return $this; } @@ -136,14 +148,14 @@ public function convert($database = null) * * @return mixed */ - protected function compile() + protected function compile($table) { // Grab the template $template = File::get(__DIR__ . '/stubs/seed.stub'); // Replace the classname - $template = str_replace('{{className}}', ucfirst(Str::camel($this->database)) . 'DatabaseSeeder', $template); - $template = str_replace('{{run}}', $this->seedingStub, $template); + $template = str_replace('{{className}}', ucfirst(Str::camel($table)) . 'DatabaseSeeder', $template); + $template = str_replace('{{run}}', $this->seedingStub[$table], $template); return $template; } @@ -152,10 +164,19 @@ protected function compile() private function insertPropertyAndValue($prop, $value) { - $prop = addslashes($prop); - $value = addslashes($value); + if(strlen($prop) > 0) { + $prop = "'{$prop}'"; + } else { + $prop = 'null'; + } - return " '{$prop}' => '{$value}',\n"; + if(strlen($value) > 0) { + $value = str_replace("'", "\'", $value); + $value = "'{$value}'"; + } else { + $value = 'null'; + } + return " {$prop} => {$value},\n"; } //end insertPropertyAndValue() diff --git a/src/DbExporter/SeederHelper.php b/src/DbExporter/SeederHelper.php new file mode 100644 index 0000000..7c3e400 --- /dev/null +++ b/src/DbExporter/SeederHelper.php @@ -0,0 +1,49 @@ +where('id', $item['id'])->count() > 0) + { + \DB::table($table)->where('id', $item['id'])->update($item); + } + else + { + \DB::table($table)->insert($item); + } + } + else + { + $ids = collect($item)->filter(function($item, $key) { + return str_contains($key, '_id'); + })->keys()->values(); + + // If there isnt any column with _id, so check that every column matches + if($ids->count() <= 0) { + $ids = collect($item)->keys()->values(); + } + $object = \DB::table($table); + foreach ($ids as $id) { + $object = $object->where($id, $item[$id]); + } + + // save or update + if($object->count() > 0) + { + $object->update($item); + } + else + { + $object->insert($item); + } + + } + } +} diff --git a/src/DbExporter/stubs/seed.stub b/src/DbExporter/stubs/seed.stub index ceb1135..dc5f774 100644 --- a/src/DbExporter/stubs/seed.stub +++ b/src/DbExporter/stubs/seed.stub @@ -1,12 +1,16 @@