[Feature Proposal] Fluid Mode for Eloquent
I am thinking about this for some time now and yesterday Taylor (@taylorotwell) asked for feature requests on Twitter for 4.1 and beyond. So I want to propose this and hear your opinions:
Laravel makes application development super easy, fast and enjoyable. I personally think Eloquent is one of the best components of Laravel and one of the best ORMs out there in terms of usability.
But when quickly prototyping an application using Eloquent there is one thing that is not much fun, slows you down and you repeat yourself from project to project most of the time:
- Setting up your database schema and updating it all the time while you are playing around and being creative with your code. (no matter if you are using migrations or not).
While I am in this playful mood, why can't Eloquent handle this for me automatically?
Imagine you could turn on a mode I call "fluid":
// config/database.php
'fluid' => true,
Now if I create a model whose table does not exist? Eloquent will simply create it. I set an attribute which doesn't exist? Eloquent creates it. Same goes for relationships and so on. Handle as much as possible dynamically.
As soon as I am happy I will turn fluid mode off, clean() it of unused tables and attributes, maybe "lock" my schema and then can start making manual adjustments and work like we always do.
What do you guys think about this?
+1
This would be very cool. Would be nice if it also maintained or updated a related migration, or had the option via an artisan command to create one.
Sounds cool, but Eloquent doesn't know the types of its attributes, so what should it create?
I had the idea to create a package that could do this a while ago: you'd have to define the fields, types and constraints in your model, and it would automatically generate migrations with the proper schema and maybe even a basic validation service, making sure these constraints are kept. Maybe I could really do this and then integrate a feature such as this?
It wouldn't be perfect of course. In my idea it would guess the correct types based on the data you enter.
E.g. you add 5 rows with the column name "first_name" and they have a string no longer than 20 chars? -> make it VARCHAR(20)
Then you add a 6th row with 50 chars? -> update column type to VARCHAR(50)
Then you delete everything and insert 20 integers? -> update column type to INT(11)
You could introduce some conventions which you follow.
I think for the most common types it would be good enough. Naturally you will later review your schema when you turn "fluid" off.
Your idea goes in the same direction. It's not as much magic and more work upfront as my proposal but the created schema will be closer to your end product.
Maybe something to talk about in Amsterdam...
On Thu, Jun 20, 2013 at 2:09 PM, Stefan Neubig [email protected]:
It wouldn't be perfect of course. In my idea it would guess the correct types based on the data you enter.
E.g. you add 5 rows with the column name "first_name" and they have a string no longer than 20 chars? -> make it VARCHAR(20)
Then you add a 6th row with 50 chars? -> update column type to VARCHAR(50)
Then you delete everything and insert 20 integers? -> update column type to INT(11)
You could introduce some conventions which you follow.
I think for the most common types it would be good enough. You will later review your schema when you turn "fluid" off of course.
Your idea goes in the same direction. It's not as much magic and more work upfront as my proposal but the created schema will be closer to your end product.
— Reply to this email directly or view it on GitHubhttps://github.com/laravel/framework/issues/1696#issuecomment-19747280 .
Will @taylorotwell be at Laracon in Amsterdam?
:+1: +1 for this proposal. @taylorotwell is not going to be in Amsterdam I think
While this sounds cool would be a lot of extra overhead. You guys should try to do it in a package with traits or something?