PHP code example of marekskopal / orm-migrations

1. Go to this page and download the library: Download marekskopal/orm-migrations library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

marekskopal / orm-migrations example snippets



//Create DB connection
$database = new MysqlDatabase('localhost', 'root', 'password', 'database');

//Create schema
$schema = new SchemaBuilder()
    ->addEntityPath(__DIR__ . '/Entity')
    ->build();

//Create Migrator
$pathWithMigrations = __DIR__ . '/migrations/';
$migrator = new Migrator($pathWithMigrations, $database);

$migrator->generate(
    $schema,
    name: 'CreateUserTable',
);



declare(strict_types=1);

namespace MarekSkopal\ORM\Migrations\Tests\Generator\Migrations;

use MarekSkopal\ORM\Enum\Type;
use MarekSkopal\ORM\Migrations\Migration\Migration;

final class CreateUserTable extends Migration
{
    public function up(): void
    {
        $this->table('table_a')
            ->addColumn('id', Type::Int, autoincrement: true, primary: true)
            ->addColumn('name', Type::String, nullable: true, size: 255)
            ->addColumn('address', Type::String, size: 50, default: 'New York')
            ->addColumn('score', Type::Int, size: 10)
            ->addColumn('price', Type::Decimal, precision: 10, scale: 2)
            ->addColumn('type', Type::Enum, enum: ['a', 'b', 'c'], default: 'a')
            ->create();
    }

    public function down(): void
    {
        $this->table('table_a')
            ->drop();
    }
}

$migrator->migrate();