Learning Laravel: (re)Building against a legacy database

My ConferenSpy rewrite project I mentioned in this post is using the MySQL database that existed for the original – still running and being used by clients – system.  Like the title above implies, I’m learning Laravel and I want to comply with its standards and defaults as much as I can.  I ran straight into an issue with that ideal when I created my first model.  My table names are all lowercase, aren’t plural, and have underscores in them.

Being new, I found that I could place

// Set up legacy table structures
protected $table = 'conference';
protected $primaryKey = 'conference_id';

in the model to make Laravel conform to my database.

I’m moving  along with the rewrite and every model I’ve created I’ve had to change the table name and sometimes the primary key to make them work.  As I was working outside this evening I had an idea to how to move closer to the Laravel way.  The idea was, rename the actual database table to conform to Laravel naming standards, then create a view with the original table name as the view name.  It was crazy enough to work, a least for selects.  A quick google search started to let air out of my idea because to insert using a view, the syntax changes for all of the examples I looked at.

I decided to give it a try anyway, like most things I like to see the failure myself and deal from there.  I renamed the main table “conference” to “Conferences”, created a view called “conference” and grimmaced a bit as I logged into my app.

IT. WORKED.  This is freaking fantastic!

I know some of you are thinking – so what if the table names are a bit wack and don’t match up, the definition is in the model,  you can figure it out.  While that’s true, there’s just something about doing it right that I like.

My next thought is the primary key.  I don’t have it as “id” in most tables because its actually useful with spaghetti PHP to have it in the format of “tablename_id” when doing a lot of joins.  For this table, the primary key is in that format as “conference_id”.

I changed the view to rename the id column conference_id, granted creating a view to do this requires a little more work typing out all the fields, but it’s worth it if it works.

SELECT id as conference_id,
FROM `Conferences`

I tested this by logging into my app again and creating a new conference, and then editing a conference, and changing conferences to edit, PERFECT! I was stunned actually, its so simple, and no one ever explains how to do this if you wanted to, mostly because if you’re learning you’re creating migrations to build out your database.  Its probably an edge case to keep an older database with a rewrite (typical me, edge case), but whatever, this is GOLD. (can you tell how excited I am?)

My Conferences table now complies with Laravel naming standards so my new code is compliant. The app that clients are using still works which is really more important than anything.  And I know now that I can do this to all of my tables, and from here on out, models will be right.

I’ll call that a good evening of learning and experimentation.

Code it right

posted in Laravel by Jonathan

Follow comments via the RSS Feed | Leave a comment | Trackback URL

Leave Your Comment

Powered by Wordpress and MySQL. Theme by Shlomi Noach, openark.org