Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
systems:yii2:modules [2018/03/30 10:51]
smayr [Using Custom Database]
systems:yii2:modules [2018/04/18 09:56] (current)
smayr [Using Custom Database]
Line 61: Line 61:
          'class' => 'app\modules\product\Module',          'class' => 'app\modules\product\Module',
          'components' => [          'components' => [
 +              // Main application database
               'db' => [               'db' => [
-                    'tablePrefix' => 'module_',  // override main 'db' component attributes+                    'class' => 'yii\db\Connection',
               ],               ],
 +              // Custom module database
 +              //'db-mod-product' => [
 +              //    'class'       => 'yii\db\Connection',
 +              //    'dsn'         => 'mysql:host=localhost;dbname=acme_product',
 +              //    'username'    => 'dbuser',
 +              //    'password'    => 'secret',
 +              //    'charset'     => 'utf8',
 +              //    'tablePrefix' => 'mod_',
 +              //],
           ],           ],
       ],       ],
Line 212: Line 222:
          'components' => [          'components' => [
               // Option 1: custom database for module               // Option 1: custom database for module
-              //'db-mod-label' => require(__DIR__ . '/db-mod-label.php'),  +              // 
 +              // In @app/config/db-mod-label.php: 
 +              //'db-mod-label' => require(__DIR__ . '/db-mod-label.php'), 
 +              //   
 +              // In @app/modules/label/config/db-mod-label.php: 
 +              //'db-mod-label' => require(__DIR__ .  
 +              //    DIRECTORY_SEPARATOR . '..' .  
 +              //    DIRECTORY_SEPARATOR . 'modules' .  
 +              //    DIRECTORY_SEPARATOR . 'printlog' .  
 +              //    DIRECTORY_SEPARATOR . 'config' .  
 +              //    DIRECTORY_SEPARATOR . 'db-mod-printlog.php' 
 +              //),  
                              
               // Option 2: custom database for module               // Option 2: custom database for module
Line 242: Line 263:
     {     {
         return "{{%item_product}}";  // formatted to allow table prefix (where % goes)         return "{{%item_product}}";  // formatted to allow table prefix (where % goes)
 +        
 +        // NOTE: To query the raw table name, use the following:
 +        //   echo Yii::$app->db->schema->getRawTableName(app\models\ItemProduct::tableName());
 +        // Displays as follows (assuming 'mod_' table prefix):
 +        //   mod_item_product
     }     }
          
Line 254: Line 280:
        // Get custom connection from module (preferable).        // Get custom connection from module (preferable).
        // In Yii 2.0.13 and newer, it is preferable to use $module->get('db')        // In Yii 2.0.13 and newer, it is preferable to use $module->get('db')
-       // instead of getting a connection from app (using Yii::$app->get())+       // instead of getting a connection from app (using Yii::$app->get('db'))
        $module = \Yii::$app->controller->module;        $module = \Yii::$app->controller->module;
-       return $module->get("db-mod-{$module->id}");   // use private module database+       return $module->get("db-mod-label");   // use private database for module 'label'
     }     }
          
Line 264: Line 290:
  
 == References == == References ==
-  * [[http://www.yiiframework.com/doc-2.0/guide-structure-modules.html|Yii 2.0 Guide: Creating Modules]]+  * [[http://www.yiiframework.com/doc-2.0/guide-structure-modules.html|Yii 2 Guide: Creating Modules]] 
 +  * [[https://stackoverflow.com/questions/27254540/multiple-database-connections-and-yii-2-0|Multiple Database Connections in Yii 2]] 
 +  * [[https://stackoverflow.com/questions/34051383/yii2-modules-in-advanced-template|Yii 2 Modules in Advanced template]] 
 +  * [[https://www.yiiframework.com/doc/guide/2.0/en/concept-autoloading|Yii 2 Class Autoloading and Namespaces]] 
 +  * [[https://www.yiiframework.com/doc/guide/1.1/en/basics.namespace|Yii 1.1 Namespaces]] 
 +  * [[https://github.com/yiisoft/yii2/issues/3647|Better configuration of frontend, backend, and modules]] 
 +  * [[https://yii2-cookbook.readthedocs.io/structure-backend-frontend-modules/|Yii 2 Cookbook: Structure of Backend/Frontend Modules]]