PHP Classes

File: gii/default_data/data_model.php

Recommend this page to a friend!
  Classes of Insolita   YII2 Migrik   gii/default_data/data_model.php   Download  
File: gii/default_data/data_model.php
Role: Example script
Content type: text/plain
Description: Example script
Class: YII2 Migrik
Create migration files for applications using YII2
Author: By
Last change:
Date: 1 year ago
Size: 1,807 bytes
 

Contents

Class file image Download
<?php
/**
 * This view is used by console/controllers/MigrateController.php
 * The following variables are available in this view:
 * @var $migrationName string the new migration class name
 * @var insolita\migrik\gii\DataGenerator $generator
 */

echo "<?php\n";
?>

use yii\db\Exception;
use yii\db\Migration;
use yii\helpers\VarDumper;
use yii\helpers\Console;
use \yii\base\Event;
use <?=$generator->modelClass?>;

class <?= $migrationName ?> extends Migration
{
    public function init()
    {
        $this->db = 'db';
        parent::init();
    }

    public function safeUp()
    {
        /**
        Uncomment this block for detach model behaviors
        Event::on(<?=$generator->modelBasename?>::className(), <?=$generator->modelBasename?>::EVENT_INIT,
                 function(Event $event ){
                     $event->sender->detachBehavior('someBehaviorName');
        });
        **/
<?php foreach($generator->rawData as $attributes):?>
$model = new <?=$generator->modelBasename?>();
        $model->setAttributes(
            <?=\yii\helpers\VarDumper::export($attributes)?>,
        false);
        if(!$model->save()){
            $this->stderr('Fail save model with attributes '
                .VarDumper::dumpAsString($model->getAttributes()).' with errors '
                .VarDumper::dumpAsString($model->getErrors()));
                throw new Exception('Fail save $model');
        }
<?php endforeach;?>
}

    public function safeDown()
    {
        //$this->truncateTable('<?= ($generator->usePrefix)?$generator->tableAlias:$generator->tableName ?> CASCADE');
        //<?=$generator->modelBasename?>::deleteAll([]);
    }

    protected function stderr($message)
    {
        Console::output(Console::ansiFormat($message, [Console::FG_PURPLE]));
    }
}