7

I am building a product that is based on the Yii2 advanced template.

As part of this product and its future deployments, I am trying to automatically create the tables related to Authorization in a regular Yii2 migration.

E.g, when the end user installs the product and runs the regular Yii migration commands he should have a fully functional user management AND authorization active.

For authorization to work, the Yii2 RBAC documentation page states that 4 tables are needed (auth_*). The documentation states that they are created by running the following migration:

yii migrate --migrationPath=@yii/rbac/migrations

I'd like to offset this extra hassle from the end user by running this specific migration code for him inside a regular migration that will be stored in common/migrations.

Any easy solution for this?

Dzhuneyt
  • 8,437
  • 14
  • 64
  • 118

4 Answers4

4

I have created a migrate.sh file where I put my migration commands that I need to run. This allows me to migrate from multiple places in the same time. It is quite simple, take a look here: https://github.com/Mihai-P/yii2-app-advanced/blob/master/migrate.sh

Instead of running ./yii migrate/up i just run sh migrate.sh that will update everything from any place.

The actual point of this is: you do not have to stick to exactly what Yii gave you. That is just a template for you to build on. Fork it, modify it, make it your own.

Mihai P.
  • 9,307
  • 3
  • 38
  • 49
  • The idea of building a single centralized script or a .sh file is nice. I was looking at the problem from a different angle. Thanks for the solution. Marked as accepted. – Dzhuneyt Mar 11 '15 at 10:47
  • I have built other things in the past but this is what was eventually requested by the guy that does the automatic deployment. Being a shell script allows you to make deployment quite easily. – Mihai P. Mar 11 '15 at 20:47
3

Try to add in console/config/main.php:

'controllerMap' => [
        'migrate' => [
            'class' => 'yii\console\controllers\MigrateController',
            'migrationPath' => [
                '@console/migrations',               
                '@yii/rbac/migrations',
            ]
        ]
    ],
gamover
  • 13
  • 3
Tahiaji
  • 420
  • 8
  • 15
0

Another approach (not using *.sh file) is to copy the rbac_init migration to your migrations folder:

cp vendor/yiisoft/yii2/rbac/migrations/m???????_rbac_init.php console/migrations/

Now, when you run php yii migrate it will be included the rbac_init migration.

sdlins
  • 2,195
  • 1
  • 23
  • 31
-1

I know this is pretty old question, but here is easy solution, create migration file and have this code in that file

<?php
require(Yii::getAlias('@yii/rbac/migrations/m140506_102106_rbac_init.php'));

/**
 * Class m220225_133725_init_rbac
 */
class m220225_133725_init_rbac extends m140506_102106_rbac_init
{

}
Hayk
  • 1
  • 1