Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I write migrations to insert records using phinx?

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?

like image 225
igrossiter Avatar asked Feb 01 '15 17:02

igrossiter


People also ask

How do I create a migration in Phinx?

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.

What is a Phinx migration?

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.


2 Answers

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()
    {

    }
}
like image 53
miholeus Avatar answered Oct 25 '22 07:10

miholeus


  1. Run this command to generate StatusMigration class:
php vendor/bin/phinx create StatusMigration
  1. Edit that file like:
<?php

use Phinx\Migration\AbstractMigration;

class StatusMigration extends AbstractMigration
{
    public function change()
    {
        $this->table('status')
            ->addColumn('code', 'string')
            ->addColumn('description', 'string')
            ->create();
    }
}
  1. You can use Phinx's database seeding mechanism to insert rows into tables. First run following command:
php vendor/bin/phinx seed:create StatusSeeder

It will generate StatusSeeder.php file in Phinx's seeds folder.

  1. Edit StatusSeader.php like:

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();
    }
}
  1. Now, run following commands to create table and seed data:
php vendor/bin/phinx migrate
php vendor/bin/phinx seed:run
like image 45
Ghorban M. Tavakoly Avatar answered Oct 25 '22 08:10

Ghorban M. Tavakoly