I'm using phinx to handle the migration on a new project, now I need to create a new table and insert some rows to it, I have:
$tableStatus = $this->table('status');
$tableStatus->addColumn('code', 'string');
$tableStatus->addColumn('description', 'string');
$tableStatus->save();
This add the new table but I couldn't find at the documentation how to insert rows, but it seems possible:
The AbstractMigration Class All Phinx migrations extend from the AbstractMigration class. This class provides the necessary support to create your database migrations. Database migrations can transform your database in many ways such as creating new tables, inserting rows, adding indexes and modifying columns.
It is possible? How can I do it?
Phinx Installation You can add Phinx to any PHP project using composer. The first command creates a folder in your current directory, php-migrations , and the second command moves into it. The last command starts an interactive shell.
Phinx makes it ridiculously easy to manage the database migrations for your PHP app. In less than 5 minutes, you can install Phinx using Composer and create your first database migration. Phinx is just about migrations without all the bloat of a database ORM system or application framework.
You can do it. Read documentation for more information.
http://docs.phinx.org/en/latest/migrations.html#executing-queries
use Phinx\Migration\AbstractMigration;
class MyNewMigration extends AbstractMigration
{
/**
* Migrate Up.
*/
public function up()
{
// execute()
$count = $this->execute('insert into users(id, name) values (5, "john")');
}
/**
* Migrate Down.
*/
public function down()
{
}
}
php vendor/bin/phinx create StatusMigration
<?php
use Phinx\Migration\AbstractMigration;
class StatusMigration extends AbstractMigration
{
public function change()
{
$this->table('status')
->addColumn('code', 'string')
->addColumn('description', 'string')
->create();
}
}
php vendor/bin/phinx seed:create StatusSeeder
It will generate StatusSeeder.php file in Phinx's seeds folder.
use Phinx\Seed\AbstractSeed;
class StatusSeeder extends AbstractSeed
{
public function run()
{
$data = [
['code' => 'c1', 'description' => 'Some description'],
['code' => 'c2', 'description' => 'Another description'],
];
$this->table('status')
->insert($data)
->save();
}
}
php vendor/bin/phinx migrate
php vendor/bin/phinx seed:run
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With