I' m developing a plugin for october cms. But I have yet a database with table on mySQL. Now I'm looking for a methods to autogenerate "models" based on the database table.
I' m looking for somethings like the laravel command
php artisan code:models
to work on the plugin.
thanks
I believe what youre looking for is:
php artisan create:model Acme.Blog Post
The create:model command generates the files needed for a new model. The first parameter specifies the author and plugin name. The second parameter specifies the model class name.
reference: https://octobercms.com/docs/console/scaffolding#scaffold-create-model
Related
I'm getting started with PHP and I wanted to know if there is a way that you can extract data from the DB in a model.
If I have a Users table in my DB, I'm looking for something like Users.all in rails that will use the model to extract all the entires in the Database.
Does PHP offer that type of functionality?
The closest to Rails is Laravel. Laravel's database layer Eloquent was already mentioned in the comment. Eloquent: Getting Started
If you go for an ORM instead of Eloquent's active record pattern Doctrine is the most popular project. Doctrine Website
Pure php doesn't provide an ORM out of the box ( I suppose you can install a package or something), but if you are using Laravel (since the tag in your question), you need to create a model (supposing you have a migration for users_table)
php artisan make:model user
then simply 'use' the model in any controller
use App\User
and get all users :
$users = User::all();
How can I create Laravel Model dynamically or without php artisan command?
Alongside creating the model dynamically, how to crate the table and the rows for it?
I cannot be able to find the exact solution anywhere!
Thanks!
My question is if it is possible to add all the fields directly to a new model via Eloquent.
I guess it would be something like
php artisan make:model MyModel --fields=?
However, I can't find anything related with that. Anyway, I have to generate a lot of model and any trick would be really appreciated.
Thanks in advance
If you mean table's column by fields then:
Firstly you don't need to define fields in modal. I mean in Laravel no need to define fields while creating model. Besides, model automatically work with your database table's columns as its property.
So, now you may want to define columns while creating migration, not while creating model. There is library to serve this demand named as Generator(https://github.com/laracasts/Laravel-5-Generators-Extended) maintained by Laracasts.
Using this generator you can generate migration file to create table in DB specifying their column names and their type also. Here is a example from their Github repo, how you can do this:
php artisan make:migration:schema create_users_table --schema="username:string, email:string:unique"
You can checkout their documentation for more information. Best of luck.
It's not possible with make:model or make:migrations commands, but you can create your own console command and add this feature by yourself.
Also, take a look at source code of make:model and make:migration commands to get some ideas on how to do that.
it looks like only built in options are --migration and -m to include a migration with the model generation. L5.3 Docs
There does look like there is a package for L5.0, which looks like it would work in 5.*+. It is put out by Laracasts:
https://github.com/laracasts/Laravel-5-Generators-Extended
It also looks like you can make a custom solution as well:
https://laracasts.com/discuss/channels/tips/l5-artisan-command-makemodel
Hope that helps!
No options while creating a model,
This is my terminal output (laravel 5.3) while i check,
You don't need to mention fields while creating model.
Ex:- based on the rules you should keep the names as like below,
model name as User
table name as users
then the model automatically handle everything, you don't need to mention the table/fields name.
I was looking for the same thing myself, as I used to work like that in previous frameworks, but could not find it, or at least not as I wanted it, so I did my thing. You can check it out if you like:
https://github.com/Triun/laravel-model-base
It will read your database, and create the laravel eloquent models for you.
It is meant to be very flexible, so the configuration may be a little complex, and I guess that I didnt' catch up with the documentation, but you can ask me if you don't know how to make it do what you want.
Basically it has 4 customization levels:
By out of the box modificators, configurable by the config files.
Including interfaces and traits to your auto-generated models.
Create your own modificators. Classes where you receive the model skeleton before it is saved, so you can add or remove properties, methods, etc.
Generate the model base, but edit yourself the final model.
And, of course, suggestions and contributions are more than welcome.
I'm starting to use the console migrations for CakePHP.
I would like to know where should I put the Initial Data for my application. For example if I'm going to run it in a developer machine for the first time and I need to set up some tables with data.
As I can see in the official book they recommend the "CakeSchema callbacks", but the method "public function after()" inside schema.php is rewritten every time i run:
cake schema generate
Also this doesn't look like a clean approach.
Where should I put this kind of instruction?
I'm running CakePHP 2.4
Thanks!
You can use the Migrations plugin for such a thing https://github.com/CakeDC/migrations
So that you can provide the migrations (creation of tables, creation of fields, as well insertion of data into your tables)
Hey.
I'm having a hard time migrating changes I've done i my config/doctrine/schema.yml file.
I added the column age to the user table. Then I did a php symfony doctrine:generate-migrations-diff followed by php symfony doctrine:migrate .
Looking in my database, the column age is now added, without deleting any data.
But, my /lib/model/doctrine/base/BaseUser.class.php is not changed, there is no age field or functions for age . So I also did the command php symfony doctrine:build-model . Finally the model is updated/migrated too.
So I wonder, is this the only way? Seems like a lot of work, and I'm afraid to miss something each time doing it.
Could I go right into phpmyadmin, add changes in the database there and just do a php symfony doctrine:build-schema , and like that skip the migration part (two commands).
Also when the comes to use of models, am I right that /lib/model/doctrine/User.class.php is where I can make functions and such for my User "data class"? Like, making a function isFemale . If not, where would that kind of function be?
This might be a bad question, but why is the model layer inside the /lib/doctrine path? As far as I have learned, you keep modules inside apps, where you create your view and controller. Why should the model be outside. Like this I can make models without attached controller and view?
Thanks.
Why should the model be outside
Because models can be used everywhere in your project, in example, in different applications and modules.
Could I go right into phpmyadmin, add changes in the database there and just do a php symfony doctrine:build-schema , and like that skip the migration part (two commands).
Of course you can, but migrations are a good approach to track your schema when deploying to production or working in team.
Here how I use doctrine migrations (simple use-case):
Add a column age to my User model in schema.yml
./symfony doctrine:generate-migrations-diff. Migration class(-es) have been generated.
./symfony doctrine:migrate. Column age successfully added to table.
./symfony doctrine:build --all-classes. Build forms/filters/models
That's it. The main idea is that doctrine:generate-migrations-diff class:
Gathers information about all your models' structure (php-representation of schema.yml)
Compares your schema.yml and info from (1)
Generates migration classes based on difference
Also when the comes to use of models, am I right that /lib/model/doctrine/User.class.php is where I can make functions and such for my User "data class"? Like, making a function isFemale . If not, where would that kind of function be?
Yes, you can add such method to User model because it's about users.