protected function setupDbData() { /** @var \yii\db\Connection $db */ $db = Yii::$app->getDb(); $migration = new Migration(); /** * Create tables **/ // Company $db->createCommand()->createTable('company', ['id' => $migration->primaryKey(), 'name' => $migration->string()->notNull()->unique()])->execute(); // User $db->createCommand()->createTable('user', ['id' => $migration->primaryKey(), 'username' => $migration->string()->notNull()->unique()])->execute(); // Project $db->createCommand()->createTable('project', ['id' => $migration->primaryKey(), 'name' => $migration->string()->notNull(), 'company_id' => $migration->integer()->notNull()])->execute(); $db->createCommand()->createIndex('company_id-name', 'project', 'company_id,name', true)->execute(); $db->createCommand()->createTable('link', ['language' => $migration->string(5)->notNull(), 'name' => $migration->string()->notNull(), 'link' => $migration->string()->notNull(), 'link_type_id' => $migration->integer(), 'PRIMARY KEY(language, name)'])->execute(); $db->createCommand()->createTable('link_type', ['id' => $migration->primaryKey(), 'name' => $migration->string()->notNull()->unique()])->execute(); $db->createCommand()->createTable('project_link', ['language' => $migration->string(5)->notNull(), 'name' => $migration->string()->notNull(), 'project_id' => $migration->integer()->notNull(), 'PRIMARY KEY(language, name, project_id)'])->execute(); // Project User $db->createCommand()->createTable('project_user', ['project_id' => $migration->integer()->notNull(), 'user_id' => $migration->integer()->notNull(), 'PRIMARY KEY(project_id, user_id)'])->execute(); // Dummy $db->createCommand()->createTable('dummy', ['id' => $migration->primaryKey(), 'parent_id' => $migration->integer()])->execute(); /** * Insert some data */ $db->createCommand()->batchInsert('company', ['id', 'name'], [[1, 'Apple'], [2, 'Microsoft'], [3, 'Google']])->execute(); $db->createCommand()->batchInsert('user', ['id', 'username'], [[1, 'Steve Jobs'], [2, 'Bill Gates'], [3, 'Tim Cook'], [4, 'Jonathan Ive']])->execute(); $db->createCommand()->batchInsert('project', ['id', 'name', 'company_id'], [[1, 'Mac OS X', 1], [2, 'Windows 10', 2]])->execute(); $db->createCommand()->batchInsert('link_type', ['id', 'name'], [[1, 'public'], [2, 'private']])->execute(); $db->createCommand()->batchInsert('link', ['language', 'name', 'link', 'link_type_id'], [['fr', 'mac_os_x', 'http://www.apple.com/fr/osx/', 1], ['en', 'mac_os_x', 'http://www.apple.com/osx/', 1]])->execute(); $db->createCommand()->batchInsert('project_link', ['language', 'name', 'project_id'], [['fr', 'mac_os_x', 1], ['en', 'mac_os_x', 1]])->execute(); $db->createCommand()->batchInsert('project_user', ['project_id', 'user_id'], [[1, 1], [1, 4], [2, 2]])->execute(); }
public function getMenu() { $gitignore = join(DIRECTORY_SEPARATOR, [\Yii::getAlias('@app'), '.gitignore']); $array = [["text" => "Copy .htaccess to @app/web", "checked" => true, "disabled" => function (ConsoleCheckBox $item) { $dir = [\Yii::getAlias('@app'), 'web', '.htaccess']; return file_exists(join(DIRECTORY_SEPARATOR, $dir)); }, "exec" => function (ConsoleCheckBox $item) { $source = [\Yii::getAlias('@vendor'), 'carono', 'yii2-components', 'templates', '.htaccess']; $dist = [\Yii::getAlias('@app'), 'web', '.htaccess']; return copy(join(DIRECTORY_SEPARATOR, $source), join(DIRECTORY_SEPARATOR, $dist)); }], ["text" => "Add db.php to @app/.gitignore", "checked" => true, "disabled" => function (ConsoleCheckBox $item) use($gitignore) { $lines = explode("\n", file_get_contents($gitignore)); return array_search('db.php', $lines) || array_search('/config/db.php', $lines); }, "exec" => function (ConsoleCheckBox $item) use($gitignore) { file_put_contents($gitignore, "\n/config/db.php", FILE_APPEND); return true; }, "error" => !file_exists($gitignore)], ["text" => "Create currency table", "checked" => true, "disabled" => function (ConsoleCheckBox $item) { return (bool) Yii::$app->db->getTableSchema('currency'); }, "exec" => self::migrate('@carono/migrations/m151216_093214_currency')], ["text" => "Create cities table", "id" => "cities", "checked" => true, "disabled" => (bool) Yii::$app->db->getTableSchema('city'), "exec" => self::migrate('@carono/migrations/m151216_084006_cities')], ["text" => "Create RBAC (@yii/rbac/migrations)", "checked" => true, "disabled" => function (ConsoleCheckBox $item) { return (bool) Yii::$app->db->getTableSchema('auth_item'); }, "exec" => self::migrate('@yii/rbac/migrations')], ["text" => "Create 'file_upload' table", "checked" => true, "disabled" => (bool) Yii::$app->db->getTableSchema('file_upload'), "exec" => self::migrate('@carono/migrations/m151127_104851_file_upload_table')], ["text" => "Create 'company' table (RUS)", "checked" => true, "disabled" => (bool) Yii::$app->db->getTableSchema('company'), "exec" => self::migrate('@carono/migrations/m160222_202733_company')], ["text" => "Create 'User' table", "id" => "user", "checked" => true, "disabled" => (bool) Yii::$app->db->getTableSchema('user'), "exec" => function (ConsoleCheckBox $item) { $command = new Migration(); $table = ['user' => ['id' => $command->primaryKey(), 'login' => $command->string(), 'hash' => $command->string(), 'activation_code' => $command->string(), 'recover_code' => $command->string(), 'last_logon' => $command->dateTime(), 'access_token' => $command->string(), 'created' => $command->dateTime(), 'updated' => $command->dateTime(), 'active' => $command->boolean()->notNull()->defaultValue(false)], 'address' => ['id' => $command->primaryKey(), 'raw' => $command->string(), 'postcode' => $command->string(), 'street' => $command->string(), 'build' => $command->string(), 'structure' => $command->string(), 'flat' => $command->string(), 'registration_date' => $command->date()]]; if ($item->owner->findById('cities')->value) { $table["address"]["city_id"] = [$command->integer(), 'city', 'id']; } if ($item->findById('photos')->value) { $table["user"]["photos"] = [$command->pivot(), 'file_upload', 'id', 'photo_id']; } if ($item->findById('personal')->value) { $table["user"]["personal_id"] = [$command->integer(), 'personal', 'id']; $table["personal"] = ['id' => $command->primaryKey(), 'email' => $command->string(), 'first_name' => $command->string(), 'second_name' => $command->string(), 'patronymic' => $command->string(), 'birth_date' => $command->date(), 'sex' => $command->boolean(), 'phone' => $command->string(), 'avatar_id' => [$command->integer(), 'file_upload', 'id'], 'legal_address_id' => [$command->integer(), 'address', 'id'], 'real_address_id' => [$command->integer(), 'address', 'id'], 'updated' => $command->dateTime()]; } else { $table["user"] = array_merge($table["user"], ['email' => $command->string(), 'first_name' => $command->string(), 'second_name' => $command->string(), 'patronymic' => $command->string(), 'birth_date' => $command->date(), 'sex' => $command->boolean(), 'phone' => $command->string(), 'legal_address_id' => [$command->integer(), 'address', 'id'], 'real_address_id' => [$command->integer(), 'address', 'id']]); } $index = [["user", "login", true], ["user", "access_token", true]]; $command->upTables($table); $command->upFk($command->collectFks($table)); $command->upIndex($index); return true; }, "items" => [["id" => "personal", "text" => "User profile as relation", "checked" => true, "inherit" => true], ["id" => "photos", "text" => "Create with photos", "checked" => true, "inherit" => true], ["id" => "user_model", "text" => "Copy default User model", "checked" => false, "exec" => function () { $source = [\Yii::getAlias('@vendor'), 'carono', 'yii2-components', 'templates', 'User.php']; $dist = [\Yii::getAlias('@app'), 'models', 'User.php']; return copy(join(DIRECTORY_SEPARATOR, $source), join(DIRECTORY_SEPARATOR, $dist)); }]]]]; return $array; }
/** * @param bool|true $autoIncrement * @param null $length * @param bool|true $unsigned * @return string */ public function primary($autoIncrement = true, $length = null, $unsigned = true) { return parent::integer($length === null ? 10 : $length) . ($unsigned ? ' unsigned' : '') . ' NOT NULL' . ($autoIncrement ? ' AUTO_INCREMENT' : '') . ' PRIMARY KEY'; }
/** * Create next row migration as ColumnSchemaBuilder * @param array $config * @return ColumnSchemaBuilder */ protected function buildNextField($config) { /** @var ColumnSchemaBuilder $row */ $row = null; $length = isset($config['length']) && $config['length'] ? $config['length'] : null; $precision = isset($config['precision']) && $config['length'] ? $config['precision'] : null; $scale = isset($config['scale']) && $config['length'] ? $config['scale'] : null; $this->migrationClass = new Migration(['db' => $this->db]); if (!(isset($config['type']) && $config['type'])) { throw new \ErrorException("Type field is undefined"); } switch ($config['type']) { case Schema::TYPE_PK: $row = $this->migrationClass->integer($length); break; case Schema::TYPE_UPK: $row = $this->migrationClass->integer($length); break; case Schema::TYPE_INTEGER: $row = $this->migrationClass->integer($length); break; case Schema::TYPE_BIGINT: $row = $this->migrationClass->bigInteger($length); break; case Schema::TYPE_BIGPK: $row = $this->migrationClass->bigInteger($length); break; case Schema::TYPE_UBIGPK: $row = $this->migrationClass->bigInteger($length); break; case Schema::TYPE_BINARY: $row = $this->migrationClass->binary($length); break; case Schema::TYPE_CHAR: $row = $this->migrationClass->char($length); break; case Schema::TYPE_TEXT: $row = $this->migrationClass->text(); break; case Schema::TYPE_DATE: $row = $this->{$this}->migrationClass->date(); break; case Schema::TYPE_DECIMAL: $row = $this->migrationClass->decimal($precision, $scale); break; case Schema::TYPE_DOUBLE: $row = $this->migrationClass->double($precision); break; case Schema::TYPE_FLOAT: $row = $this->migrationClass->float($precision); break; case Schema::TYPE_DATETIME: $row = $this->migrationClass->dateTime($precision); break; case Schema::TYPE_MONEY: $row = $this->migrationClass->money($precision, $scale); break; case Schema::TYPE_SMALLINT: $row = $this->migrationClass->smallInteger($length); break; case Schema::TYPE_TIME: $row = $this->migrationClass->time($precision); break; case Schema::TYPE_TIMESTAMP: $row = $this->migrationClass->timestamp($precision); break; default: $row = $this->migrationClass->string($length); break; } if (isset($config['type']) && ($config['type'] == Schema::TYPE_UPK || $config['type'] == Schema::TYPE_UBIGPK) || isset($config['unsigned']) && $config['unsigned']) { $row = $row->unsigned(); } if (isset($config['default']) && !empty($config['default'])) { $row = $row->defaultValue($config['default']); } if (isset($config['is_not_null']) && $config['is_not_null']) { $row = $row->notNull(); } if (isset($config['comment']) && $config['comment']) { $row = $row->comment($config['comment']); } if (isset($config['isCompositeKey']) && $config['isCompositeKey']) { $this->primaryKeys[] = $config['name']; } $config['related_table'] = isset($config['related_table']) ? trim($config['related_table']) : null; $config['related_field'] = isset($config['related_field']) ? trim($config['related_field']) : null; if ($config['related_table'] && $config['related_field']) { $this->relations[] = ['fk_name' => isset($config['fk_name']) && $config['fk_name'] ? $config['fk_name'] : self::getNameForeignKey($this->tableNameRaw, $config['related_table'], $config['name'], $config['related_field'], 255), 'table_name' => $this->addTablePrefix($this->tableNameRaw), 'field' => $config['name'], 'related_table' => $this->addTablePrefix($config['related_table']), 'related_field' => $config['related_field']]; } return $row; }