You are viewing revision #1 of this wiki article.
This version may not be up to date with the latest version.
You may want to view the differences to the latest version.
My CMS supports multiple sites using one code base therefore the db connection can only be configured by detecting the site's domain.
As Yii's MigrateCommand class does not support a domain parameter and configuring the db connection in the console.php is impossible in this case, I have extended the MigrateCommand as below.
class XMigrateCommand extends MigrateCommand
{
public $domain = '';
protected $_db;
public function beforeAction($action,$params)
{
if (in_array(strtolower($action),array('up','down','safeup','safedown','redo','to','new','history','makr')) && $this->domain == '')
{
echo 'You need to specify the domain as Xpress support multi-sites and multi-database.',"\n",'i.e: --domain=www.domain.com',"\n";
return false;
}
return parent::beforeAction($action,$params);
}
protected function getDbConnection()
{
if($this->_db!==null)
return $this->_db;
else
{
// environment file locates in /sites/env folder using this naming convention: config.domain.php
$envFile = 'config.'.$this->domain.'.php';
$envFile = Yii::app()->basePath.'/../sites/env/'.$envFile;
if (file_exists($envFile))
{
include ($envFile);
$db = $dbs['default'];
$this->_db = new CDbConnection($db['connectionString'],$db['username'],$db['password']);
return $this->_db;
}
else
{
echo "Error: {$envFile} is not found. Invalid domain or this domain is not configured properly.";
exit(1);
}
}
echo "Error: CMigrationCommand.connectionID '{$this->connectionID}' is invalid. Please make sure it refers to the ID of a CDbConnection application component.\n";
exit(1);
}
}
With this class, you can run migrate commands adding the --domain=... parameter. You will need to modify the code in getDbConnection to suite your need considering:
- My env file is located in sites/env folder with this naming convention: config..php, i.e. config.abc.com.php
- In the env file, there are many db connections defined. Each is a item of the $dbs[ ]
$dbs['default']['connectionString'] = 'mysql:...';
$dbs['default']['username'] = 'username';
$dbs['default']['password'] = 'password';
Thanks
looks fine ...
If you have any questions, please ask in the forum instead.
Signup or Login in order to comment.