PHP code example of tecnocen / yii2-migrate

1. Go to this page and download the library: Download tecnocen/yii2-migrate 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/ */

    

tecnocen / yii2-migrate example snippets


abstract class EntityTable extends \tecnocen\migrate\CreateTableMigration
{
    public function defaultColumns()
    {
        return [
            'created_at' => $this->datetime()->notNull(),
            'updated_at' => $this->datetime()->notNull(),
            'created_by' => $this->normalKey(),
            'updated_by' => $this->normalKey(),
        ]          
    }

    public function defaultForeignKeys()
    {
        return [
            'created_by' => 'user',
            'updated_by' => 'user',
        ];
    }
}

abstract class PivotTable extends \tecnocen\migrate\CreateTableMigration
{
    public function defaultColumns()
    {
        return [
            'created_at' => $this->datetime()->notNull(),
        ]          
    }
}

class m170101_010101_ticket extends EntityTable
{
     public function columns()
     {
         return [
             'id' => $this->primaryKey(),
             'project_id' => $this->normalKey(),
             'title' => $this->string(150)->notNull(),
             'description' => $this->text(),
         ];
     }

     public function foreignKeys()
     {
         return ['project_id' => ['table' => 'project']];
     }
}

class m17010101_010102_ticket_employee extends PivotTable
{
     public function columns()
     {
         return [
             'ticket_id' => $this->normalKey(),
             'employee_id' => $this->normalKey(),
         ];
     }

     public function foreignKeys()
     {
         return [
             'ticket_id' => ['table' => 'ticket'],
             'employee_id' => ['table' => 'employee'],
         ];
     }

     public function compositePrimaryKeys()
     {
         return ['ticket_id', 'employee_id'];
     }
}

use tecnocen\migrate\CreateViewMigration;
use common\models\Ticket;

class m17010101_010101_ticket_details extends CreateViewMigration
{
    /**
     * @inheritdoc
     */
    public function viewName()
    {
       return 'ticket_details';
    }
    
    /**
     * @inheritdoc
     */
    public function viewQuery()
    {
        return Ticket::find()
            ->alias('t')
            ->innerJoinWith([
                'project' => function ($query) {
                    $query->alias('p');
                },
            ])->select([
                'ticket_id' => 't.id',
                'project_id' => 'p.id',
                'ticket_title' => 't.title',
                'ticket_description' => 't.description',
                'project_name' => 'p.name',t
            ]);
    }
}