我正在构建一个CRM系统,该系统应该允许不同用户进行不同的访问,因此我需要在面板中进行复杂的身份验证和通知。但主系统应该使用REDIS。我已经安装了yii2 redis插件,并在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',
];
在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,
我如何区分我的模型,使用mysql或redis?我假设如果我在组件部分指定redis,整个应用程序使用redis db。我可以使用具有多个值的redis作为密钥,但由于redis在达到最大内存时丢弃了密钥,所有注册用户及其配置文件都会显示出来,所以我认为我应该使用mysql这个basci的东西(带有角色的数据库,验证,个人资料)
答案 0 :(得分:0)
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',
],
],
];
然后尝试使用
// 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();
对于模型中的活动记录,写入
// db1
public function getDb() {
return Yii::$app->db1;
}
//Or db2
public function getDb() {
return Yii::$app->db2;
}