I found the solution and adding it below, Hope it will help other.
- First of all. Create 4 fields in users table in your master database. with name like dbhost, dbusername, dbpwd, and dbname, So that each user will have their separate database.
- Create a dump of current database and save that in .sql file and keep it inside your upload/dump folder on root.
- Use below code when user is signup in system.
private function createUserDatabase($user_id) {
try{
$userModel = User::findOne($user_id);
if($userModel->dbname != NULL && $userModel->dbname != “”){
return true;
}
$path = Yii::getAlias(’@webroot’).’/uploads/dump’;
$sqlDumpPath = $path.’/userdatabase.sql’;
$databaseName = "userdb_".$user_id."_".time();
$db = Yii::$app->getDb();
$dbUserName = $db->username;
$dbPassword = $db->password;
$dbHost = $this->getDsnAttribute('host', $db->dsn);
$con = mysqli_connect($dbHost, $dbUserName, $dbPassword);
if(! $con ){
echo 'Connected failure<br>';
exit;
}
$query = "CREATE DATABASE `".$databaseName."`";
if(mysqli_query($con, $query)){
echo "Database $databaseName created successfully";
//$command='mysql -h' .$dbHost .' -u' .$dbUserName .' -p' .$dbPassword .' ' .$databaseName .' < ' .$sqlDump;
$command="(D:\wamp\bin\mysql\mysql5.7.21\bin\mysql --user=$dbUserName --password=$dbPassword --host=$dbHost --database $databaseName < $sqlDumpPath) 2>&1";
exec($command,$output,$worked);
if($worked==0) {
$userModel->db_host =$dbHost;
$userModel->db_port ="";
$userModel->db_user =$dbUserName;
$userModel->db_pwd =$dbPassword;
$userModel->dbname =$databaseName;
$userModel->save();
echo "Information saved successfully";
}else{
$dropquery = "DROP DATABASE `".$databaseName."`";
mysqli_query($con, $dropquery);
}
Yii::$app->session->setFlash('success', "Database created successfully");
}
else {
Yii::$app->session->setFlash('error', "Database creation failed");
}
}catch(Exception $e){
$dropquery = "DROP DATABASE IF EXISTS `".$databaseName."`";
mysqli_query($con, $dropquery);
Yii::$app->session->setFlash('error', $e->getMessage());
}
return true;
}
private function getDsnAttribute($name, $dsn)
{
if (preg_match('/' . $name . '=([^;]*)/', $dsn, $match)) {
return $match[1];
} else {
return null;
}
}
add these functions in your required controller, May be in siteController. It will create new database for user.
4) Create a new model inside frontend/models with name save UserActiveRecord.php and add below code.
<?php
namespace frontend\models;
use Yii;
use yii\data\ActiveDataProvider;
use yii\web\Session;
use \DateTime;
class UserActiveRecord extends \yii\db\ActiveRecord
{
public static function getDb() {
return Yii::$app->db2;
}
}
?>
-
And then in each of your model replace extends \yii\db\ActiveRecord with extends UserActiveRecord.
-
In index.php add below code.
$application= (new yii\web\Application($config));
if (!Yii::$app->user->isGuest) :
$userData = Yii::$app->user->identity;
$user_id = $userData->id;
$host = $userData->db_host;
$db_user = $userData->db_user;
$db_pwd = $userData->db_pwd;
$db_name = $userData->dbname;
$dsn = “mysql:host=$host;dbname=$db_name”;
Yii::$app->db2->close();
\Yii::$app->db2->dsn = $dsn;
\Yii::$app->db2->username = $db_user;
\Yii::$app->db2->password = $db_pwd;
\Yii::$app->db2->charset = ‘utf8’;
\Yii::$app->db2->open();
endif;
$application->run();