triasrahman/laravel-jsonseeder

suggestion: Automatically detect classname and infer databasename

Opened this issue · 0 comments

FBnil commented

You should be able to get the classname with get_class()
Then, remove _TableSeeder, or assume the name is the full classname

$jsonfilename = str_replace( '_TableSeeder', '', get_class($this));

Also, in $this->JSONSeed($tablename,$tablemodel), I can confuse the fact I need the table name because I already have the model? You can get the tablename from the model like so:

$n = new App\Mytablemodel; $n->getTable();

And if I do:

$this->JSONSeed('tablename.js', '\App\Tablename')

Well, the first argument is not the filename (which I thought at first)... so it fails.

And the Model seems to be required, please add this in the documentation as a required (kind of a bummer, even if a model is very quickly created). But kudo's to the fact that you can reformat the JSON into multilines, that helps with versioning and diff's.

This is what I have been using (it also feeds different files, depending on the language. This is the kind of extra's I was expecting, but it's not there)

    public function run()
    {
                $for = str_replace( 'TableSeeder', '', get_class($this));
                $for = strtolower($for);
                $lang = config('app.locale');
                $jsonfile = $for.'-'.$lang.'.json';
                $file = base_path() ."/storage/database/" . $jsonfile;
                if( !file_exists ($file) )
                        dd("FATAL: $for: Language '$lang' has no translation. See $file");
                $json_arr = json_decode(file_get_contents($file), true);
                if($json_arr){
                        DB::table($for)->delete();
                        DB::table($for)->insert( $json_arr );
                }else{
                        dd("ERROR in $file, please check syntax and try again");
                }
    }

Thus so far I have the following ideas:

  • get tablename-$locale.js or fallback to tablename.js to allow optional language support
  • use get_class() to assume more and the need to type less
  • Model is optional (but then, data embedded from other tables is not possible). But that's ok for a normal, no foreign keys, table (keep simple things simple)
  • The use JSONSeeder can be omitted if extends Seeder becomes extends JSONSeeder (and if run() is removed, you do not need to even type the line inside run())
  • relations or realtions?
  • Is it not more logical for JSONSeed() to accept an array with optional fields? [ 'file' => $filename, 'relations' => $relations, 'table'=>$tablename] ?

In conclusion: Useful module. But it does not save much typing code yet.