How to retrieve component value directly

I’d need some advice on how I can simplify some code in a way I can simply gets it’s value anywhere in the db (controller, views) with a single call instead of what I am currently using.

Basically, I need to be able to check against which type of db the application is connecting to use the appropriate queries, etc…

I was doing

$db = Yii::$app->getDb();
$dbdsn = explode(';', $db->dsn);
$dbhost = explode('=', $dbdsn[0]);
$dbname = explode('=', $dbdsn[1]);
$dbtype = explode(':', $db->dsn);

and using

$dbtype[0]

to see if the application is using ‘mysql’ or ‘sqlsrv’

Is there some way to simply be able to do something along the lines of

Yii::$app->getDb->serverType

So I can do

if(Yii::$app->getDb->serverType == 'mysql'){
   //
}else{
   //
}

As always, thank you for taking the time to help!

Yii::$app->db->getDriverName() should be ok in your case.

2 Likes

Right on the money! Thank you.