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 $insert * @param array $changedAttributes * @return bool */ public function afterSave($insert, $changedAttributes) { parent::afterSave($insert, $changedAttributes); if ($insert) { $migrate = new Migration(); $tableOptions = ''; if ($migrate->db->driverName === 'mysql') { $tableOptions = 'CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE=InnoDB'; } $table = self::getTable($this->tab_index, $this->tab); $tableOptions = $tableOptions . " COMMENT '" . Yii::t('app', 'Comment prefix') . $this->name . "'"; $migrate->createTable($table, ['id' => $migrate->primaryKey()], $tableOptions); return true; } }