PHP code example of achrafsoltani / migration-service-provider

1. Go to this page and download the library: Download achrafsoltani/migration-service-provider 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/ */

    

achrafsoltani / migration-service-provider example snippets


$app->register(new MigrationServiceProvider(), array(
    'migration.path' => __DIR__.'/../src/Resources/migrations',
    'migration.register_before_handler' => true,
    'migration.migrations_table_name'   => 'migration_version',
    'migration.db' => $app['db']
));



// I'm using a custom namespace with a custom loader
namespace Core\User\Migration;

use Doctrine\DBAL\Schema\Schema;
use AchrafSoltani\Migration\AbstractMigration;
use Silex\Application;

// Custom class, demonstration only
use Core\User\Model\User;

/**
 * Class UsersMigration
 *
 * @package Core\User\Migration
 */
class UsersMigration extends AbstractMigration
{
    /**
     * @param Schema $schema
     */
    public function schemaUp(Schema $schema)
    {
        // before app is started, do the following.
        $table = $schema->createTable('users');
        $table->addColumn('id', 'integer', array(
            'unsigned'      => true,
            'autoincrement' => true
        ));
        $table->addColumn('username', 'string', array('notnull' => true, 'default' => '', 'length' => 100));
        $table->addColumn('password', 'string', array('notnull' => true, 'default' => '', 'length' => 255));
        $table->addColumn('roles', 'string', array('notnull' => true, 'default' => '', 'length' => 255));
        $table->setPrimaryKey(array("id"));
        $table->addUniqueIndex(array("username"));
    }
    
    public function appUp(Application $app)
    {
        // create default user
        // My model manager class takes a Doctrine\DBAL\Connection instance as parameter
        // This part is fully custom, for demontration purpose only
        $user_model = new User($app['db']);
        $user_data = array(
            'username' => 'achraf',
            'password' => $app['security.encoder.digest']->encodePassword('password', ''),
            'roles'    => 'ROLE_USER'
        );
        $user_model->create($user_data);
    }
    
    public function getMigrationInfo()
    {
        return 'Added users table';
    }
}

// Very important to add if you wanna organise your migration files within namespaced folders
// if not specified, the migration will be expected to be in the \Migration namespace
return __NAMESPACE__;
 {.php}
ilex\Application;
use Silex\Provider\DoctrineServiceProvider;
use Gridonic\Provider\ConsoleServiceProvider;
use AchrafSoltani\Provider\MigrationServiceProvider;

$app = new Application();

$app->register(new Silex\Provider\DoctrineServiceProvider(), array(
    'db.options' => array(
        // db options
    ),
));

$app->register(new ConsoleServiceProvider(), array(
    // console options
));

// Usage

$app->run();