D
D
Denis Ogurtsov2014-08-07 19:38:42
Yii
Denis Ogurtsov, 2014-08-07 19:38:42

How to get database name in YII2?

I need to raise a migration to create a table.
The table will have a field that will be a foreign key to a field from another database.
I fixed database1.fieldit and everything worked.
But I need dynamic.
From model i can get table name
common\models\GoogleGroups::tableName();
but how to get database name from model?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander Zelenin, 2014-08-07
@DenisOgr

private function getDsnAttribute($name, $dsn)
    {
        if (preg_match('/' . $name . '=([^;]*)/', $dsn, $match)) {
            return $match[1];
        } else {
            return null;
        }
    }

Usage anywhere:
Usage in the model:
then push the function into some helper, but if you write a migration, then why do you need a model?
In migration:
$this->getDsnAttribute('dbname', $this->db->dsn);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question