Where to get the name of the database?


because we’re doing cross-database joins with ActiveRecord, which works pretty nice, btw.

We need the tableName() prefixed with the database name.

So where can I get this info?

at the moment it looks like:

    public function tableName() {

        // return table name with DB name to get relations from different DBs to work

        $name = preg_match("/dbname=([^;]*)/", $this->dbConnection->connectionString, $matches);

        return $matches[1].'.PiiFile';


Which is nothing but a ugly hotfix.

Best regards,


the only way i see it is to extend the CDbConnection class and add another property to it called ‘dbName’, in the config you set that name. after that you will be able to access it $this->dbConnection->dbName

You can extend CDbConnection class and add properties $driverName, $dbName and $hostName.

Then rewrite it’s open() method:

protected function open()


    $this->connectionString = $this->driverName.':host='.$this->hostName.';dbname='.$this->dbName;



Now, set in the config file properties driverName, dbName and hostName, and access them anywhere in the application this way:


Didn’t test it all, but it doesn’t look too sophisticated to don’t work :)

Note, that you’ll limit yourself a little to specific DBMS - no matter if you extract it from the DSN or use separate attributes. Not every system uses the concept of db host + db name. Think about SQLite where you’d have a filename instead. That’s probably the reason why a DBMS specific DSN is used instead.

Thanks for your answers.

So I think, I’ll simply use a param for that problem.

public function tableName() {

        return Yii::app()->params['tableNamePrefix'].'PiiFile';


Should be 100% compatible and you simply can skip setting the param if you do not need cross-database joins.

A good way if you’re using MySQL is do a SELECT DATABASE() to get the current database.

i got it this way

$curdb  = explode('=', Yii::app()->db->connectionString);

echo $curdb[2];