I want to setup module's database independent from base project and configure it from config/web.php like the following:
'modules' => [
'user' => [
///...
'db' => [
'class' => 'yii\db\Connection',
'dsn' => 'pgsql:host=127.0.0.1;port=5432;dbname=klabs',
'username' => 'postgres',
'password' => '',
'charset' => 'utf8',
]
]
]
Is it available to make so? And how to do it, if yes?
First, you need config other database components like that:
...
'components' => [
'db1' => [
'class' => 'yii\db\Connection',
'dsn' => 'mysql:host=localhost;dbname=db1name',
'username' => 'db1username',
'password' => 'db1password',
],
'db2' => [
'class' => 'yii\db\Connection',
'dsn' => 'pgsql:host=localhost;port:5432dbname=db2name',
'username' => 'db2username',
'password' => 'db2password',
],
],
...
And choose db component you defined before in your module config:
'modules' => [
'user' => [
'class' => 'other\to\UserModule',
'layout' => 'main',
'db' => 'db2', // or db1
...
],
],
Hope it helpful.
Goodluck and have fun!
Related
I keep getting this error when I'm trying to open Album application from Laminas MVC tutorial. I use multicontainer configuration in Docker which consists of linked containers. These are laminas-mvc-tutorial container and mysql database container. PDO mysql is enabled but I think it is something with my Adapter configuration issues.
Here is global.php config array:
use Laminas\Db\Adapter;
return [
'service_manager' => [
'abstract_factories' => [
Adapter\AdapterAbstractServiceFactory::class
],
'factories' => [
Adapter\AdapterInterface::class => Adapter\AdapterServiceFactory::class,
],
'aliases' => [
Adapter\Adapter::class => Adapter\AdapterInterface::class
]
],
'db' => [
'driver' => 'Pdo',
'adapters' => [
mysqlAdapter::class => [
'driver' => 'Pdo',
'dsn' => 'mysql:dbname=Laminas;host=localhost;charset=utf8',
'username' => 'root',
'port' => '3306',
'password' => 'pass1234',
'driver_options' => [
PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES \'UTF8\''
],
],
],
],
];
As far as I am aware. All of this:
'abstract_factories' => [
Adapter\AdapterAbstractServiceFactory::class
],
'factories' => [
Adapter\AdapterInterface::class => Adapter\AdapterServiceFactory::class,
],
'aliases' => [
Adapter\Adapter::class => Adapter\AdapterInterface::class
]
Is not needed. If you are following the docs why are you not in development mode instead of production? Since in development mode this should be read from the local.php file instead of global.php?
'db' => [
'driver' => 'Pdo',
'dsn' => 'mysql:dbname=your_dbname;host=localhost;charset=utf8',
'username' => 'your_username',
'password' => 'your_password',
],
Which is literally the only thing needed to connect to MySQL for laminas/laminas-db
The error you get:
'Laminas\Db\Adapter\Laminas\Db\Adapter\AdapterInterface' not found
I think there is a namespace issue.
Try to use only:
use Laminas\Db;
insted of the one you're currently using.
I'm building a CRM system which should allow a different access for different users, so i need to make a complex authentication and notifications in the panel. But the main system should use the REDIS. I have installed yii2 redis plugin and have the config like this in db.php (mysql config ):
<?php
return [
'class' => 'yii\db\Connection',
'dsn' => 'mysql:host=localhost;dbname=yii2basic',
'username' => 'root',
'password' => '',
'charset' => 'utf8',
// Schema cache options (for production environment)
//'enableSchemaCache' => true,
//'schemaCacheDuration' => 60,
//'schemaCache' => 'cache',
];
And in web.php:
$db = require __DIR__ . '/db.php';
$config = [
'id' => 'basic',
'basePath' => dirname(__DIR__),
'bootstrap' => ['log'],
'aliases' => [
'#bower' => '#vendor/bower-asset',
'#npm' => '#vendor/npm-asset',
],
'components' => [
'redis' => [
'class' => 'yii\redis\Connection',
'hostname' => 'localhost',
'port' => 6379,
'database' => 0,
],
],
'db' => $db,
How can i differntiate my models, to use either mysql or redis? I assume if i specify redis in components section, the whole app uses redis db. I could use redis with multiple values for key, but since redis is dropping the keys when max memory will be reached, all registered users and their profiles will parish, so i think i should use mysql for this basci stuff ( database with roles, authentification, profiles)
return [
'components' => [
'db1' => [
'class' => 'yii\db\Connection',
'dsn' => 'mysql:host=localhost;dbname=db1name', //maybe other dbms such as psql,...
'username' => 'db1username',
'password' => 'db1password',
],
'db2' => [
'class' => 'yii\redis\Connection',
'dsn' => 'YOURCONFIG',
'port' => '6379',
'password' => 'db2password',
'username' => 'db2username',
],
],
];
Then try to use
// To get from db1
Yii::$app->db1->createCommand((new \yii\db\Query)->select('*')->from('tbl_name'))->queryAll()
// To get from db2
Yii::$app->db2->createCommand((new \yii\db\Query)->select('*')->from('tbl_name'))->queryAll();
For active record in your model write
// db1
public function getDb() {
return Yii::$app->db1;
}
//Or db2
public function getDb() {
return Yii::$app->db2;
}
I am using PostgreSQL and the default database schema in my Yii2 application.
I created a new schema called laboratory and I need to define it in the common/config/main-local.php file.
This is my current main-local.php file:
<?php
return [
'components' => [
'db' => [
'class' => 'yii\db\Connection',
'dsn' => 'pgsql:host=localhost;dbname=travel',
'username' => 'aaaa',
'password' => 'bbbb',
'charset' => 'utf8',
],
],
];
How can I add the laboratory schema in this file? I need both schemas.
Is Yii2 supporting multiples schemas?
You can configure more than one in components
return [
'components' => [
'db1' => [
'class' => 'yii\db\Connection',
'dsn' => 'mysql:host=localhost;dbname=testdb1',
'username' => 'demo1',
'password' => 'demo1',
],
'db2' => [
'class' => 'yii\db\Connection',
'dsn' => 'mysql:host=localhost;dbname=testdb2',
'username' => 'demo2',
'password' => 'demo2',
],
],
];
and you can refer to each one using
\Yii::$app->db1;
or
\Yii::$app->db2;
http://www.yiiframework.com/doc-2.0/guide-db-active-record.html
http://www.yiiframework.com/doc-2.0/guide-start-databases.html
for postgresql you could try
return [
'components' => [
'db1' => [
'class' => 'yii\db\Connection',
'dsn' => 'pgsql:host=localhost;dbname=testdb1',
'username' => 'demo1',
'password' => 'demo1',
'schemaMap' => [
'pgsql'=> [
'class'=>'yii\db\pgsql\Schema',
'defaultSchema' => 'your_schema1' //specify your schema here
]
],
],
'db2' => [
'class' => 'yii\db\Connection',
'dsn' => 'mysql:host=localhost;dbname=testdb2',
'username' => 'demo2',
'password' => 'demo2',
'schemaMap' => [
'pgsql'=> [
'class'=>'yii\db\pgsql\Schema',
'defaultSchema' => 'your_schema2' //specify your schema here
]
],
],
],
];
It wasn't necessary to change it:
<?php
return [
'components' => [
'db' => [
'class' => 'yii\db\Connection',
'dsn' => 'pgsql:host=localhost;dbname=travel',
'username' => 'aaaa',
'password' => 'bbbb',
'charset' => 'utf8',
],
],
];
Then Gii code generator recognizes the laboratory schema (but autocomplete for Table Name doesn't work).
I'm going through Security-Authorization Tutorial to configure authManager using DbManager.
After declaring the below code in web.php
<?php
$params = require(__DIR__ . '/params.php');
$config = [
'id' => 'basic',
'basePath' => dirname(__DIR__),
'bootstrap' => ['log'],
'components' => [
'urlManager' => [
'showScriptName' => false,
'enablePrettyUrl' => true
],
'authManager' => [
'class' => 'yii\rbac\DbManager',
],
'request' => [
// !!! insert a secret key in the following (if it is empty) - this is required by cookie validation
'cookieValidationKey' => 'tYXyeisqgn9Qn_baaI6JRV4a6NY54nrq',
],
'cache' => [
'class' => 'yii\caching\FileCache',
],
'user' => [
'identityClass' => 'app\models\User',
'enableAutoLogin' => true,
],
'errorHandler' => [
'errorAction' => 'site/error',
],
'mailer' => [
'class' => 'yii\swiftmailer\Mailer',
// send all mails to a file by default. You have to set
// 'useFileTransport' to false and configure a transport
// for the mailer to send real emails.
'viewPath' => '#backend/mail',
'useFileTransport' => true,
'transport' => [
'class' => 'Swift_SmtpTransport',
'host' => 'localhost',
'username' => 'root',
'password' => '',
'port' => '8080',
'encryption' => 'tls',
],
],
'log' => [
'traceLevel' => YII_DEBUG ? 3 : 0,
'targets' => [
[
'class' => 'yii\log\FileTarget',
'levels' => ['error', 'warning'],
],
],
],
'db' => require(__DIR__ . '/db.php'),
],
'params' => $params,
];
if (YII_ENV_DEV) {
// configuration adjustments for 'dev' environment
$config['bootstrap'][] = 'debug';
$config['modules']['debug'] = [
'class' => 'yii\debug\Module',
];
$config['bootstrap'][] = 'gii';
$config['modules']['gii'] = [
'class' => 'yii\gii\Module',
];
}
return $config;
And, this code in console.php
<?php
Yii::setAlias('#tests', dirname(__DIR__) . '/tests');
$params = require(__DIR__ . '/params.php');
$db = require(__DIR__ . '/db.php');
return [
'id' => 'basic-console',
'basePath' => dirname(__DIR__),
'bootstrap' => ['log', 'gii'],
'controllerNamespace' => 'app\commands',
'modules' => [
'gii' => 'yii\gii\Module',
],
'components' => [
'authManager' => [
'class' => 'yii\rbac\DbManager',
],
'cache' => [
'class' => 'yii\caching\FileCache',
],
'log' => [
'targets' => [
[
'class' => 'yii\log\FileTarget',
'levels' => ['error', 'warning'],
],
],
],
'db' => $db,
],
'params' => $params,
];
config/db.php
<?php
return [
'class' => 'yii\db\Connection',
'dsn' => 'mysql:host=localhost;dbname=danishYii',
'username' => 'root',
'password' => '',
'charset' => 'utf8',
];
I typed ./yii migrate --migrationPath=vendor/yiisoft/yii2/rbac/migrations
in my terminal. This command i got from Component is not getting loaded -Stack overflow
I got this error in my terminal
Exception 'yii\db\Exception' with message 'SQLSTATE[HY000] [2002]
Can't connect to local MySQL server through socket
'/var/run/mysqld/mysqld.sock' (2)'
I'm very new to Yii. So please don't mind if this is a silly question.
Please help me to rectify this problem.
Check if in your
console/config/main.php
you have a proper configuration for db access like this sample for yii2-app-advanced template
'components' => [
'db' => [
'class' => 'yii\db\Connection',
'dsn' => 'mysql:host=your_hostname;dbname=your_dbname',
'username' => 'your_username',
'password' => 'your_password',
'charset' => 'utf8',
'enableSchemaCache' => true,
],
for basic template
be sure you have in basic/console/config.php a reference to db in component secion like this
return [
.......
'components' => [
......
'db' => require(__DIR__ . '/db.php'),
],
......
];
and then in basci/config/db.php a properly db config
return [
'class' => 'yii\db\Connection',
'dsn' => 'mysql:host=your_hostname;dbname=your_dbname',
'username' => 'your_username',
'password' => 'your_password',
'charset' => 'utf8',
];
I read this question yii 2.0 multiple database connection, and use the answer of #Ali MasudianPour.
I follow the first step:
First you need to configure your databases like below:
return [
'components' => [
'db1' => [
'class' => 'yii\db\Connection',
'dsn' => 'mysql:host=localhost;dbname=database1',
'username' => 'root',
'password' => '',
'charset' => 'utf8',
],
'db2' => [
'class' => 'yii\db\Connection',
'dsn' => 'mysql:host=localhost;dbname=database2',
'username' => 'root',
'password' => '',
'charset' => 'utf8',
],
],
]; ?>
But in my configuration in my db it gives me this error:
The configuration for the "db" component must contain a "class"
element.
This is because db component is main and required and you simply omitted its declaration.
Rename db1 and db2 for example to db and db1 accordingly:
return [
'components' => [
// Main connection
'db' => [
'class' => 'yii\db\Connection',
'dsn' => 'mysql:host=localhost;dbname=database1',
'username' => 'root',
'password' => '',
'charset' => 'utf8',
],
// Another connection
'db1' => [
'class' => 'yii\db\Connection',
'dsn' => 'mysql:host=localhost;dbname=database2',
'username' => 'root',
'password' => '',
'charset' => 'utf8',
],
],
]; ?>
Update:
Note that for the basic application db is configured in separate file config/db.php and then required in main config config/web.php like so:
'db' => require(__DIR__ . '/db.php'),
So you can configure main connection in db.php and add additional below as db1.
Simply you just have to create separate file for each database .
config/db ->
'class' => 'yii\db\Connection',
'dsn' => 'mysql:host=localhost;dbname=database1',
'username' => 'root',
'password' => '',
'charset' => 'utf8',
To access another db create file in config named db1.php add add another db configuration.
config/db1 ->
'class' => 'yii\db\Connection',
'dsn' => 'mysql:host=localhost;dbname=database2',
'username' => 'root',
'password' => '',
'charset' => 'utf8',
In config->web.php add
'db' => require(__DIR__ . '/db.php'),
'db1' => require(__DIR__ . '/db1.php'),
To access the Model:
public static function getDb() {
return Yii::$app->db1;
}