Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Phinx Seeders & Faker
<?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();
}
}
<?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();
}
}
<?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