Skip to content

PHP API

Run the actions of the package from PHP code, with the options of each method.

Run migrations from specific migrations directory

<?php

use Phalcon\Migrations\Migrations;
use Phalcon\Migrations\Utils\Config;

$config = Config::fromArray([
    'database' => [
        'adapter'  => 'mysql',
        'host'     => 'phalcon-db-mysql',
        'username' => 'root',
        'password' => 'root',
        'dbname'   => 'vokuro',
    ],
]);

Migrations::run([
    'directory'      => __DIR__,
    'migrationsDir'  => [
        __DIR__ . '/migrations',
    ],
    'config'         => $config,
    'migrationsInDb' => true,
]);

The config value must be a Phalcon\Migrations\Utils\Config object. Use Config::fromArray() to create it from an array with the database and application sections. If you use a plain array, Migrations::run() throws an exception with the message Internal error. Config should be an instance of Phalcon\Migrations\Utils\Config.

Migrations::run() does not read the log-in-db key of the configuration. Use the migrationsInDb key.

Migrations::run() accepts these options:

Key Type Description
config Config Database configuration (required)
migrationsDir array / string Migrations directories (required)
directory string Project directory, which holds the file log. Default: the BASE_PATH constant, or the parent directory of APP_PATH. Set it if you do not define these constants
version string Version to migrate to. Default: the latest version
tableName string Table name, comma-separated table names or a prefix with an asterisk. Default: @ (all tables)
tsBased bool true - use timestamp based versions
migrationsInDb bool true - keep the migrations log in the database. false - keep it in a file. If you do not set it, the package uses the file log and gives a deprecation notice
log-path string Path of the log file (file log). Default: .phalcon/migration-version in the project directory
log-table string Name of the log table (database log). Default: phalcon_migrations
verbose bool true - show debugging information
skip-foreign-checks bool true - disable the foreign key checks during the run
truncate bool true - empty each table before batchInsert() imports its data

Other actions in PHP

The other actions also have a static method in Phalcon\Migrations\Migrations. Each method needs the config key, with a Phalcon\Migrations\Utils\Config object.

Migrations::generate()

Key Type Description
config Config Database configuration (required)
migrationsDir array Migrations directories (required)
tableName string Table name, comma-separated table names or a prefix with an asterisk. @ - all the tables (required)
version string Version to generate. Default: the next version
force bool true - overwrite the files of an existing version
tsBased bool true - generate a timestamp based version
descr string Description of a timestamp based version
exportData string always or oncreate. See Export data
exportDataFromTables array Tables to export data from
noAutoIncrement bool true - leave out the AUTO_INCREMENT value of the table
skip-ref-schema bool true - leave out the schema of the referenced table in the foreign keys
namespace string Namespace of the generated migration classes
log-table string Name of the log table, which generate skips. Default: phalcon_migrations
verbose bool true - dry run. generate writes no files. The --dry option of the CLI sets this key

Migrations::listAll()

Key Type Description
config Config Database configuration (required)
migrationsDir array Migrations directories (required)
tsBased bool true - use timestamp based versions (required)

listAll() also accepts the directory, migrationsInDb, log-path and log-table keys of Migrations::run().

Migrations::clean()

Key Type Description
config Config Database configuration (required)
tableName string Comma-separated table names. Default: @ (all the tables)
data-only bool true - delete the rows and keep the tables

clean() also accepts the directory, migrationsInDb, log-path and log-table keys of Migrations::run().

Migrations::export()

Key Type Description
config Config Database configuration (required)
migrationsDir array / string Migrations directories (required)
version string Version to export. Default: the newest version
tableName string Comma-separated table names. Default: @ (all the tables)
tsBased bool true - use timestamp based versions

export() prints the SQL. See Export the schema.

<?php

use Phalcon\Migrations\Migrations;
use Phalcon\Migrations\Utils\Config;

$config = Config::fromArray([
    'database' => [
        'adapter'  => 'mysql',
        'host'     => '127.0.0.1',
        'username' => 'root',
        'password' => '',
        'dbname'   => 'shop',
    ],
]);

Migrations::generate([
    'config'        => $config,
    'migrationsDir' => [__DIR__ . '/db/migrations'],
    'tableName'     => 'invoices,customers',
]);
Navigation

Type to search…

↑↓ navigate↵ selectEsc close