Jump to content
Robert1995

Laravel 4 Experience - Database Migration from Code.

Recommended Posts

Posted

L-am scris mai devreme in engleza, poate va ajuta.

So, today I've started my next product in Laravel.

I've worked around 8 hours to get a good installer done, just wanted to point out some tips for other people who are going to "pursue it".

If you're going to use migrations, I got this code after 4 hours of research, and I mean it, it was almost impossible to find an exact match of it.

Sometimes the Symfony Command Line needed me to define the STDIN myself. So I added a little check, also the Laravel Migrate asks you if "Are you sure" if you're running it in production. So I had to find a way past this, and eventually I found the "--force" parameter, which has to be used like this, there was no code example from PHP how to set it, only by command line, so after 15 minutes of staring at some migration functions, I've "understood" that it actually has to be the key parameter, not the value like this array("--force")


// STDIN Fallback, we need that.
if(!defined("STDIN"))
define('STDIN',fopen("php://stdin","r"));

Artisan::call('migrate', array("--force" => true));

Also, before doing that a database connection has to be done, after I've dissected all the available products & softwares I could get my hands on, they were all hardcoding mySQL only. And I wanted to make my installer let you chose between any Database Connections available, mySQL, sqlite, pgsql, sqlsrv .

So after asking the users for their "settings", I've "parsed" them like this into the config, so the database connection could actually work.


Config::set('database.default', $receivedInformation['connection_driver']);

foreach($receivedInformation['connection'][$receivedInformation['connection_driver']] as $key => $value)
Config::set('database.connections.' . $receivedInformation['connection_driver'] . '.' . $key, $value);

Hope this helped :).

Good luck !

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



×
×
  • Create New...