Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Query] Is there a way to generate all resources from an existing database? #120

Open
skills-up opened this issue Jun 17, 2019 · 5 comments

Comments

@skills-up
Copy link

Hi,

Is there a way to generate all the resources from an existing database?

I know we can generate resources pertaining to individual tables using:
php artisan resource-file:from-database [model-name]

What I wish to know is if there is any command like:
php artisan resource-file:from-database --all-tables
to generate resources for all the tables in the database with a single command?

This will be really helpful for generating one click demos/applications from a schema, without the need to write a separate script to iterate through the database, and generating corresponding commands in the shell.

Thanks,
Gaurav

@MikeAlhayek
Copy link
Collaborator

MikeAlhayek commented Jun 18, 2019

It’s been a while since I used it, but please tryphp artisan create:mapped-resources.

The mapped-resources command is designed to allow you to create multiple resources all specified at once.

If you created resources using this package, most likely you already have resources/laravel-code-generator/sources/resources_map.json created. Update that file directly by adding resources to it. Once you add a new entry for each resource you want to create, call php artisan create:mapped-resources and all the resources should get created for you.

This command could be improved in the future to allow you to generate resource for every table on a given database. But in most cases, you’ll never want a CRUD for every table which is why the mapped-resource command allow you to only add what you really need.

You may also want to review the documentation on how to correctly modify the map file https://crestapps.com/laravel-code-generator/docs/2.2 The documentation has two related sections, once talks about the command and the second talk about the file’s format and what commands available there

I hope this helps.

PS: kindly use stackoverflow.com for question where github is for bugs or new future requests.

@dbilovd
Copy link

dbilovd commented Dec 5, 2019

@skills-up this is what I've used.

First, run SHOW TABLES; to get a list of all tables in your DB
Then map each table name to its model name in an array. Loop through the array and run the resource-file:from-database with each set of values.

$models = [ "tableName" => "ModelName" ];

array_walk($models, function ($model, $table) {
  $this->call('resource-file:from-database', [
    'model-name'    => $model,
    '--table-name'  => $table
  ]);
});

This will create all the necessary resource files and you can carry on from there.

NB: You can run the above code within Tinker.

@dbilovd
Copy link

dbilovd commented Dec 5, 2019

@CrestApps what do you think about the approach I suggested here #120 (comment) ?

I can submit a PR to add it as an option to the package.

@MikeAlhayek
Copy link
Collaborator

@dbilovd the problem with that approach is SHOW TABLES is mysql specific command. It won't work with other database servers.

I suggest creating a new command called resource-file:from-database-all with no required parameters. Then I would clone the resource-file:from-database command but eliminate the model-name as parameter and --database-name, --resource-filename as options.

That code in the ResourceFileFromdatabaseCommand.php has a method called getParser() which gives you parser for the connected-to database. The parser should be modified to expose a method that would return an array of all tables found on the database. To do that, you'll need to add a new abstract method on the ParserBase.php class abstract protected function getTableNames();

Then in the MySqlParser you'll implement the getTableNames() method which returns the result of SHOW TABLES command.

If you do that, please submit a PR request.

@MikeAlhayek
Copy link
Collaborator

@dbilovd you can even make the getModelName($tableName) public and then call $parser->getModelName("table-name") within your new command to get the model name following the default naming convention.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants