Open fabiomlferreira opened 4 years ago
Table introspection is slow and shouldn't be part of the already big Eloquent, IMHO this approach does not fly.
I also have this "problem" and I'm not fond of duplicating the information, but I found that with a disciplined codebase/team, there were surprisingly few (almost none, AFAIR) issues over the last half decade.
How does Yii implement this in terms of overhead?
@mfn I don't think this slow, probably I don't implement the best approach to read the fields and default values from database but this is a method that is called only when the developer whats and not on every model creation.
I don't have looked in the Yii code with detail, my loadDefaultValues() method is 100% inspired on Yii implementation but they have a getTableSchema function that I don't have looked how it works.
A side note, now that I'm starting with Laravel I found that almost everything is more complicated than Yii, some are not directly associated with Laravel but with the way the tutorials and laracasts are made.
For example developing a simples CRUD backoffice for a specific table take me about 5m or less on Yii even if the table have 20 text fields, in Laravel takes a lot more time because don't have any way to generate automatically the crud with the fields and the validations already implemented. Then in almost every example in Laracast they create a create view and a update view (this is ok) but inside each one put the form one form for update other for create this is duplication, then on the controller create 2 validation one for store other for update. At least creating admin areas there are no reason to have this separated, in most examples in Laravel on creation method we don't create any instance of the model then in the create.blade.php we need to put the input with the default values, the Yii approach is different on create view we create a new instance of the model and if we want we load the default values on the view we don't need to know what are the default values only put the fields for each attribute it it as some default value it will appear.
Don't take me wrong there are many things that Yii2 can learn from Laravel, this is the reason why I'm moving to Laravel, but in Yii2 we have a simplicity that improve alot the time we take to create simple crud operations.
Other example Yii have a Automatic Code Generation called Gii it's similar to artisan make but way more complete it reads the the schema from the database and create the views with the forms with the inputs for each field (it put type number for integers, type email if the field is called email, required if the field can't be null, etc) it also creates the validation rules if the field is required and 255 char it put it automatically on the validation, most of the times we only need small tweaks to put everything working. Other important thing in the model creation it automatically adds the phpDocs for the model, in Laravel we need to install the package laravel-ide-helper, this should be a default of artisan make, how do you know the attributes of the model that you are working on?
In resume I think artisan make should be more powerfull and have the option to create a complete crud almost automatically with validations, inputs taken from database schema, we should have the option to create the template for each view generated, this is a great help generating admin areas.
@fabiomlferreira not related to your idea, but to your comments regarding CRUD generation in your comment.
I would have a look at https://github.com/laravel-shift/blueprint it does exactly what your after I believe. It allows you to generate models, migrations, controllers, form requests, tests and probably more from a simple configuration file so you don't have to.
I work with Yii2 for some year and now that I'm starting with Laravel I found lot of features that Yii2 don't have but also find some features that Laravel should have.
When we create a migration many times we create default values for fields in Laravel we need to take that default values and put them in the model to pre-populate the form with that values
protected $attributes = [ 'type' => 2, 'order' => 0, ];
I don't like the idea of duplicate the same information on database and on the model, I think Laravel should have a method to get the defaults values from database schema and load on the model. Something like this:
$setting = new Setting(); $setting->loadDefaultValues();
This will make an extra query to the db but should not be a problem for anyone, and we only load the default values if we want.
I think this should be a method from Illuminate\Database\Eloquent\Model, but for know I have created a trait to handle this, I don't know if is the best approach but I'm starting with Laravel. I leave here the code for you inspect and give your opinion