public function up() { $table = Vote::tableName(); $this->createTable($table, ['answer_id' => $this->integer(), 'user_id' => $this->integer(), 'created_at' => $this->integer()]); MigrationHelper::addForeignKey($table, 'answer_id', Answer::tableName(), 'id', 'CASCADE', 'CASCADE'); MigrationHelper::addForeignKey($table, 'user_id', User::tableName(), 'id', 'SET NULL', 'CASCADE'); }
public function up() { $this->createTable(Category::tableName(), ['id' => Schema::TYPE_PK, 'owner_id' => $this->integer(), 'status' => Schema::TYPE_SMALLINT, 'title' => Schema::TYPE_STRING, 'name' => Schema::TYPE_STRING, 'image' => Schema::TYPE_STRING, 'content' => Schema::TYPE_TEXT, 'lft' => Schema::TYPE_INTEGER, 'rgt' => Schema::TYPE_INTEGER, 'depth' => Schema::TYPE_INTEGER]); $this->createTable(CategoryToItem::tableName(), ['category_id' => Schema::TYPE_INTEGER, 'item_id' => Schema::TYPE_INTEGER]); MigrationHelper::addForeignKey(CategoryToItem::tableName(), 'category_id', Category::tableName(), 'id', 'CASCADE', 'CASCADE'); MigrationHelper::addForeignKey(CategoryToItem::tableName(), 'item_id', Item::tableName(), 'id', 'CASCADE', 'CASCADE'); }
public function up() { $table = \app\modules\poll\models\Comment::tableName(); $this->createTable($table, ['id' => $this->primaryKey(), 'user_id' => $this->integer(), 'question_id' => $this->integer(), 'created_at' => $this->integer(), 'message' => $this->text()]); MigrationHelper::addForeignKey($table, 'user_id', \app\modules\user\models\User::tableName(), 'id', 'SET NULL', 'CASCADE'); MigrationHelper::addForeignKey($table, 'question_id', \app\modules\poll\models\Question::tableName(), 'id', 'CASCADE', 'CASCADE'); }
/** * Gets data from cache or database * Truncates tables * @return \self * @throws \yii\db\Exception */ public static function init() { if ($data = \Yii::$app->cache->get(static::$cacheKey)) { //return static::$data = unserialize($data); } $dir = \Yii::getAlias(static::$fixturePath); MigrationHelper::unsetForeignKeyCheck(); $files = FileHelper::findFiles($dir, ['only' => ['*.php']]); asort($files); foreach ($files as $file) { $table = preg_replace('/\\d+\\_(.*)/', '$1', basename($file, '.php')); \Yii::$app->db->createCommand()->truncateTable($table)->execute(); if (!($data = (require $file))) { continue; } $modelClass = @$data['modelClass']; unset($data['modelClass']); foreach ($data as $key => $values) { \Yii::$app->db->createCommand()->insert($table, $values)->execute(); if ($modelClass) { /** @var \yii\db\ActiveRecord $model */ $model = new $modelClass($values); static::$data[$table][$key] = $model->hasAttribute('id') ? $model::find()->orderBy(['id' => SORT_DESC])->one() : new $model(); } else { static::$data[$table][$key] = $values; } } } static::update(); MigrationHelper::setForeignKeyCheck(); return new static(); }
/** * @inheritdoc */ public function up() { $allPermissions = []; foreach ($this->permissions as $role => $permissions) { if (!$permissions) { continue; } $allPermissions = array_merge($allPermissions, (new Query())->from($this->autItemTable)->select('name')->where(['type' => Role::TYPE_PERMISSION, 'name' => $permissions])->column()); if ($newPermissions = array_diff($permissions, $allPermissions)) { $this->batchInsert($this->autItemTable, ['name', 'type'], MigrationHelper::toBatchData($newPermissions, [Role::TYPE_PERMISSION])); } $allPermissions = array_merge($allPermissions, $newPermissions); $this->batchInsert($this->authItemChildTable, ['child', 'parent'], MigrationHelper::toBatchData($permissions, [$role])); } }
public function up() { $this->createTable(Question::tableName(), ['id' => $this->primaryKey(), 'user_id' => $this->integer(), 'status' => $this->smallInteger(), 'type' => $this->smallInteger(), 'relation_id' => $this->string(), 'event_object' => $this->text(), 'title' => $this->string(), 'description' => $this->text(), 'created_at' => $this->integer()]); MigrationHelper::addForeignKey(Question::tableName(), 'user_id', User::tableName(), 'id', 'SET NULL', 'CASCADE'); }
public function up() { $table = Answer::tableName(); $this->createTable($table, ['id' => $this->primaryKey(), 'question_id' => $this->integer(), 'title' => $this->string(), 'value' => $this->string()]); MigrationHelper::addForeignKey($table, 'question_id', Question::tableName(), 'id', 'CASCADE', 'CASCADE'); }
/** * Adds relation data * @param $ids * @param array $defaultData * @return bool */ public function addViaIds($ids, $defaultData = []) { if (!$ids) { return true; } foreach ($ids as $key => $id) { $id = is_array($id) ? $id : [reset($this->relationAttribute) => $id]; $ids[$key] = array_merge($id, $this->condition, $defaultData); } MigrationHelper::insertUpdate($this->viaTable, array_keys(reset($ids)), $ids); }