Using both mysql and redis in yii2 application - php

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;
}

Related

Fatal error: Trait 'Laminas\Db\Adapter\Laminas\Db\Adapter\AdapterInterface' not found in /var/www/vendor/laminas/laminas-db/src/Adapter/Adapter.php

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.

Yii2: how to specify multiples database schemas?

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).

Yii2 advanced with MongoDB

I'm new to Yii2.
I want to use MongoDB with Yii2-advanced. For that, I have found this library:
https://github.com/yiisoft/yii2-mongodb
The issue is it doesn't explain much about the installation and configurations.
As the read me says I have added following into the common\config\main-local.php file:
'mongodb' => [
'class' => '\yii\mongodb\Connection',
'dsn' => 'mongodb://developer:password#localhost:27017/mydatabase',
],
But what should I do with the default db key which is there for of the MySQL connection:
'db' => [
'class' => 'yii\db\Connection',
'dsn' => 'mysql:host=localhost;dbname=yii2_advanced_mongo',
'username' => 'root',
'password' => 'root',
'charset' => 'utf8',
],
If I comment out that db key and try to login, then it gives me following error:
Invalid Configuration – yii\base\InvalidConfigException
Unknown component ID: db
I think you should add the entry properly, if you want use the entry db for accessing your db datas you should comment the db entry related to mysql and assign db to your mongodb entry
'db' => [
'class' => '\yii\mongodb\Connection',
'dsn' => 'mongodb://your_user_name:your_password#localhost:27017/your_database',
],
//'db' => [
// 'class' => 'yii\db\Connection',
// 'dsn' => 'mysql:host=localhost;dbname=yii2_advanced_mongo',
// 'username' => 'root',
// 'password' => 'root',
// 'charset' => 'utf8',
//],

How to setup module's own database

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!

Yii 2.0 : 2 database connection

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;
}

Categories