How to correctly specify migration namespace classes in yii2? - yii2

could someone explain me how can I correctly specify my modules migration namespaces? As I see in the documentation, it's:
return [
'controllerMap' => [
'migrate' => [
'class' => 'yii\console\controllers\MigrateController',
'migrationNamespaces' => [
'app\migrations', // Common migrations for the whole application
'module\migrations', // Migrations for the specific project's module
'some\extension\migrations', // Migrations for the specific extension
],
],
],
];
But there are no explanation in which file should I write the commands. I've tried it in config.php, as:
'controllerMap' => [
'migrate' => [
'class' => 'yii\console\controllers\MigrateController',
'migrationNamespaces' => [
'app\modules\adBoard\migrations',
],
But I don't know which controller class should I write. Could someone tell me in which file I have to specify it and how to specify it correctly?

If you refer to this documentation
Configuring Command Globally
Instead of entering the same option
values every time you run the migration command, you may configure it
once for all in the application configuration like shown below:
return [
'controllerMap' => [
'migrate' => [
'class' => 'yii\console\controllers\MigrateController',
'migrationTable' => 'backend_migration',
],
], ];
With the above configuration, each time you run the migration command, the backend_migration table will be used to record
the migration history. You no longer need to specify it via the
migrationTable command-line option.
Namespaced Migrations
Since 2.0.10 you can use namespaces for the
migration classes. You can specify the list of the migration
namespaces via migrationNamespaces. Using of the namespaces for
migration classes allows you usage of the several source locations for
the migrations. For example:
return [
'controllerMap' => [
'migrate' => [
'class' => 'yii\console\controllers\MigrateController',
'migrationNamespaces' => [
'app\migrations', // Common migrations for the whole application
'module\migrations', // Migrations for the specific project's module
'some\extension\migrations', // Migrations for the specific extension
],
],
], ];
this config should be placed in you console/config/main.php
but for namespaced migration remeber that starting for 2.0.10

depending on your yii2-template application (basic or advanced) the location of "console" specific settings are located in different directories.
For basic template, console fetch settings from <app>/config/console.php file.
And for advanced template, you should edit <app>/console/config/main.php file.
Remember that your settings for console will not affect web-settings, so if you want to register some component in the whole project, you have to duplicate it in both files.
P.S. I would like to add one more detail about advanced template, is that it has common setting for frontend and backend sub-apps, which is located in <app>/common/config/main.php, but those settings are not common with console commands.

I would like to share my experience with Yii2 namespaced migrations.
Scenario
I am using advanced template.
I have 100s of old migrations in console/migrations folder.
I have new extension which have namespaced migrations.
I have new migrations in old folder console/migrations.
I want to create future migrations with namespaces in new folder console/migrations/namespaced. I want to keep all old migrations intact which are located at console/migrations.
console/config/main.php configurations worked for me.
return [
'controllerMap' => [
'migrate' => [
'class' => \yii\console\controllers\MigrateController::class,
'migrationNamespaces' => [
'console\migrations\namespaced',
'yii\swiftsmser\migrations'
]
],
],
//.... more configurations
];
With above configurations, when I executed yii migrate, It includes all above mentioned folders.
Note: To create new migration. Just make sure to use command like below.
yii migrate/create console\\migrations\\namespaced\\DLTTemplatesForSMS

Related

How to change default template in Yii2?

I am using the Yii 2 Advanced Application Template, the AdminLTE Asset Bundle and the Gii code generator.
Here is my example:
I need to change the template so I can remove the "Create Lab Tipos Movimientos" button (and modify some things more).
I am removing every button after Gii create the CRUD but I would like to change the template so Gii can do it automatically.
Once you have create your own template for gii
You can change the default template for gii assigning the new template values in main.php (main-local.php)
assigning the proper parameter to gii module
.....
$config['modules']['gii'] = [
//'class' => 'yii\gii\Module',
'class' => 'your_gii_module_path\Module',
'allowedIPs' => ['127.0.0.1', '::1', ],
'generators' => [ //here
'crud' => [ // generator name
//'class' => 'yii\gii\generators\crud\Generator', // generator class
'class' => 'your_gii_module_path\generators\crud\Generator', // generator class
'templates' => [ //setting for out templates
// template name => path to template
'your_crud_entry' => 'your_absolute_path_\your_gii_module_path\generators\crud\default',
]
]
],
];
.......
I have not done it myself, but I found this Guide by SamDark in Github that explains how to create your own template. This is the url: https://github.com/yiisoft/yii2-gii/blob/master/docs/guide/topics-creating-your-own-templates.md
Additionally, if you just want to eliminate the "Create Lab Tipos Movimiento" button you can try modifying the current template which if I am not wrong is located inside the folder vendor/yiisoft/yii2-gii/generators/crud/default/views and the file should be index.php. There you can try deleting or better yet commenting the part of the code that says:
<p>
<?= "<?= " ?>Html::a(<?= $generator->generateString('Create ' . Inflector::camel2words(StringHelper::basename($generator->modelClass))) ?>, ['create'], ['class' => 'btn btn-success']) ?>
</p>
I suggest you to make a copy of the files you modify just in case anything goes wrong.
Hope this helps you.
Have a great day.
EDIT:
Additionally following the answer of schmunk to a very similar question in stack overflow found here: How to use custom templates in gii (using Yii 2)
There is apparently a Gii extension in beta phase to help you in this situation called yii2-giiant found here: https://github.com/schmunk42/yii2-giiant (maybe there are similar extensions that are in a more advanced phase of development, google search should help with that)

yii2 map extension showing "MissingKeyMapError"

I have install extension from http://www.yiiframework.com/extension/yii2-latlon-finder/
but is showing me error message "MissingKeyMapError"
I have API key but not sure where to put this.
please help me for same
As i can see, this project is last updated 2 years ago, so it can be outdated with current API's.
As a solution, i can suggest you to use 2amigos - Google Maps extension. It's pretty simple with good documentation.
After installing it, just add this to main.php config file:
'components' => [
'assetManager' => [
'bundles' => [
'dosamigos\google\maps\MapAsset' => [
'options' => [
'key' => 'this_is_my_key',
'language' => 'id',
'version' => '3.1.18'
]
]
]
],
],
Just replace this_is_my_key with your key.
The bad thing in this solution is that you will have to write some additional code to handle user input of lat/long and display it on 2amigos map.

Differentiate between Frontend and Backend Logs in DBTarget Yii2

I am logging into Db using existing Yii logging API.
But I want to differentiate between Frontend logs and Backend logs inside DB.
Everything that appears is common for both, I face difficulty tracing frontend logs.
Below is the image of DB Logs where GREEN marked are for backend logs, RED marked are for Frontend Logs.
You can use prefix property for this. This is callable that returns a string to be prefixed to every exported message with signature function ($message).
As default getMessagePrefix() is used there which prefixes the message with user IP, user ID and session ID.
You can use it to add there frontend and backend.
Thanks to #Bizley!
Inside both backend/config/main and frontend/config/main, I configured below; This is how my entire log configuration for Frontend looks like(Similarly you can do it for Backend);
'log' => [
'traceLevel' => YII_DEBUG ? 3 : 0,
'targets' => [
[
'class' => 'yii\log\DbTarget',
'levels' => ['error'],
'prefix' => function ($message) {
return "[Frontend]";
},
],
[
'class' => 'yii\log\FileTarget',
'levels' => ['error','info'],
],
],
],
Below is the view on UI for logs. With the Help of Prefix I can now easily differentiate between channels.

How to customize vendor view files?

In yii2 how do I customize vendor view files without modifying the original view files?
I'm using dektrium yii2-user and would like to make a few changes to the login page.
You can assign your view path for dektrium yii2-user in this way (assume #app your app alias) :
'components' => [
'view' => [
'theme' => [
'pathMap' => [
'#dektrium/user/views' => '#app/views/your_dir_views' // mapping for override the views dektrium with your views
],
],
.....
],

Yii2 mongodb: how to change database?

My application use many databases, they are in same structure, but data has no relation between different databases. I need to change database via request params.
In the config can only setup dsn, but I want to change database dynamically.
How can I do that.
I got myself:
$mongo = Yii::$app->get('mongodb');
$mongo->options['db'] = 'foo';
The simplest solution is to defined multiple connections in the configuration:
'components' =>
[
...
'mongodb' =>
[
'class' => '\yii\mongodb\Connection',
'dsn' => 'mongodb://localhost:27017/database1',
],
'othermongodb' =>
[
'class' => '\yii\mongodb\Connection',
'dsn' => 'mongodb://localhost:27017/database2',
],
...
]
You can then access your connections with Yii::$app->mongodb and Yii::$app->othermongodb (or using the get()-method if you prefer). This also allows you to specify the correct database for the ActiveRecord classes that come from a different database:
class MyOtherDBMongo extends \yii\mongodb\ActiveRecord
{
public static function getDb()
{
return \Yii::$app->get('othermongodb');
}
}