Created
December 1, 2015 09:40
-
-
Save robmorgan/8b293646a14b9c6c9c81 to your computer and use it in GitHub Desktop.
Phinx Seeders & Faker
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use Phinx\Migration\AbstractMigration; | |
class CreateUserTable extends AbstractMigration | |
{ | |
public function change() | |
{ | |
$users = $this->table('users'); | |
$users->addColumn('username', 'string', array('limit' => 20)) | |
->addColumn('password', 'string', array('limit' => 40)) | |
->addColumn('password_salt', 'string', array('limit' => 40)) | |
->addColumn('email', 'string', array('limit' => 100)) | |
->addColumn('first_name', 'string', array('limit' => 30)) | |
->addColumn('last_name', 'string', array('limit' => 30)) | |
->addColumn('created', 'datetime') | |
->addColumn('updated', 'datetime', array('null' => true)) | |
->addIndex(array('username', 'email'), array('unique' => true)) | |
->create(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use Phinx\Seed\AbstractSeed; | |
class UserSeeder extends AbstractSeed | |
{ | |
public function run() | |
{ | |
$data = [ | |
'username' => 'foo', | |
'password' => 'pas$w0rd', | |
'password_salt' => sha1(time()), | |
'email' => 'foo@example.com', | |
'first_name' => 'Test', | |
'last_name' => 'User', | |
'created' => date('Y-m-d H:i:s'), | |
]; | |
$users = $this->table('users'); | |
$users->insert($data) | |
->save(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use Phinx\Seed\AbstractSeed; | |
class UserSeeder extends AbstractSeed | |
{ | |
public function run() | |
{ | |
$faker = Faker\Factory::create(); | |
$data = []; | |
for ($i = 0; $i < 100; $i++) { | |
$data[] = [ | |
'username' => $faker->userName, | |
'password' => sha1($faker->password), | |
'password_salt' => sha1('foo'), | |
'email' => $faker->email, | |
'first_name' => $faker->firstName, | |
'last_name' => $faker->lastName, | |
'created' => date('Y-m-d H:i:s'), | |
]; | |
} | |
// This is a cool short-hand method | |
$this->insert('users', $data); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment