PHP code example of andrey-mokhov / anelegan-db

1. Go to this page and download the library: Download andrey-mokhov/anelegan-db 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/ */

    

andrey-mokhov / anelegan-db example snippets



return [
    // This should be an array of module namespaces used in the application.
    'modules' => [
        'Anelegan\Db', // <- added migration module
        // ... and yours modules
    ],
    // ... other settings
];


namespace Anelegan\Db\Migration;

interface MigrationInterface
{
    /**
     * @return array
     */
    public function getDependencies();

    /**
     * @return string
     */
    public function getName();

    /**
     * @return bool
     */
    public function setUp();

    /**
     * @return bool
     */
    public function tearDown();
}


namespace Application\Migration;

use Anelegan\Db\Migration\AbstractMigration;

class CreateTesting extends AbstractMigration
{
    /**
     * If your migration does not depend on other migrations, this
     * method can not determine, he has implemented in the abstract
     * class.
     * 
     * @return array
     */
    public function getDependencies()
    {
        return [];
    }

    /**
     * @return string
     */
    public function getName()
    {
        return 'testing';
    }
}


namespace Application\Migration;

use Anelegan\Db\Migration\AbstractMigration;
use Zend\Db\Sql\Ddl\Constraint\UniqueKey;

class CreateTesting extends AbstractMigration
{
    /**
     * Return migration name
     *
     * @return string
     */
    public function getName()
    {
        return 'testing';
    }

    /**
     * Create table with testing name
     *
     * @return bool
     */
    protected function safeUp()
    {
        $tableDefinition = $this->createTable('testing', [
            'id' => $this->primaryKey(),
            'name' => $this->string()->setNullable(false)->addConstraint(new UniqueKey()),
            'description' => $this->text(),
        ], ['comment' => 'Test creating table']);
        $this->execute($tableDefinition);

        return true;
    }

    /**
     * Drop table with testing name
     *
     * @return bool
     */
    protected function safeDown()
    {
        $dropTable = $this->dropTable('testing');
        $this->execute($dropTable);

        return true;
    }
}


use Application\Migration\CreateTesting;

return [
    'migration_manager' => [
        'aliases' => [
            // this key "testing" must match result of calling method (new CreateTesting)->getName();
            'testing' => CreateTesting::class, 
        ],
        'factories' => [
            CreateTesting::class => InvokableFactory::class,
        ],
    ],
];
console
# php public/index.php migrate list
 Installed migration:
    none
 Available migrations:
    > testing
console
# php public/index.php migrate up
 Available migrations:
    > testing
 Install available migration? [y/n] y
 +  Installing "initialization" migration...
    "initialization" migration successfully installed.
 +  Installing "testing" migration...
    > create table "testing"... done (time: 0.265s)
    "testing" migration successfully installed.
console
# php public/index.php migrate down
 Installed migrations:
    > testing
 Uninstall "testing" migration? [y/n]
 -  Uninstall "testing" migration...
    > drop table "testing"... done (time: 0.061s)
    "testing" migration successfully uninstalled.