Laravel 4 and database seeding

I’ve been playing around with Laravel 4 for a few days now and have been very impressed. Once I figured outhow to install it, so far the learning curve from Codeigniter seems to be small. Right now I’m taking an existing project written with Codeigniter and creating a Laravel version for it. I got the configurations set up, now to move on to the database. The migration feature is great. I’ve been able to create my database and rollback with ease. The next step is to take data from an existing database and import it over. I could dump it into an SQL file and be done with it. But the source project is live and data changes. I’d like to use migration to get the latest data off the source.

The solution is to use Laravel’s seeding. Migration and seeding work with each other. Unfortunately, the sample code and demo I’ve seen so far uses arrays to populate (seed) the database. It’s a good sign though because all I have to do is query the existing database and populate an array with the resultset. The problem is how and what the syntax is. After a few hours of researching online and testing, I’ve got the solution.

Let’s say your source database is MySQL and you have a client table with client_id and client_name columns. On your Laravel project you have a clients table with id, client_name, created_at, and updated_at columns. Add a new connection to your /app/config/database.php. If you have multiple MySQL databases, it’s okay, just append 2… so you have a mysql and mysql2 connection. So your mysql array connection holds your Laravel database and mysql2 holds your database connection information where you’ll be getting data from.

Next edit the /app/database/seeds/DatabaseSeeder.php file. Here’s what I have.

<?php
class DatabaseSeeder extends Seeder {

	/**
	 * Run the database seeds.
	 *
	 * @return void
	 */
	public function run()
	{
		Eloquent::unguard();

		$this->call('ClientTableSeeder');
	}

}

class ClientTableSeeder extends Seeder
{

	public function run()
	{
                // truncate the table before inserting imported data
		DB::table('clients')->truncate();

                // use mysql2 connection then query the database, then put resultset in array
		$clients = DB::connection('mysql2')->select('SELECT `client_id`, `client_name` FROM `client`');
		$now = date('Y-m-d H:i:s');

                // loop through the resultset and insert into laravel database
		foreach ($clients as $client)
		{
			Client::create([
				'id'			=> $client->client_id,
				'client_name' 	=> $client->client_name,
			    'created_at' 	=> $now,
			    'updated_at' 	=> $now
			]);
			
		}
	}
}

Installing Phalcon in Ubuntu

Phalcon is a PHP Framework I wanted to play with. I have recently switched one of my PC from Windows 8.1 to a Linux distro – for development and everyday use. Slowly but surely I’m finding alternative software and loving it. I’m only going to use Windows for gaming. For now, I will be using Ubuntu 14.04 LTS. The version of Phalcon for this post is 1.3.1.

There is a little guide that can be found on their website that helps install Phalcon, but just like Laravel’s instructions, it’s somewhat incomplete. Which is why, for Laravel, I had to post an add-on guide to their guide. If you follow that guide, you will first run into problems in the compilation section where they want you to run the install command. It will complain about pcre. You can read more about the error athttp://stackoverflow.com/questions/22555561/error-building-fatal-error-pcre-h-no-such-file-or-directory. The solution is also there.

Once you install libpcre3-dev, you can continue with the install. The next trouble you may run into is trying to load the phalcon extension. Again, the guide is a little vague with its instructions. The solution to this is found athttp://codingtip.blogspot.com/2014/03/install-phalcon-phalcon-developer-tool.html.

	Create phalcon.ini file in /etc/php5/apache2/conf.d/phalcon.ini add : extension=phalcon.so in this file.

Hope this helps you. I get lost easily when I play with new things. If loaded correctly, you should see this section in your php.ini.

phpinfo