To configure Yii DB MSSQL with DI container
you need to create config/common/di/db-mssql.php
configuration file:
use Psr\Log\LoggerInterface;
use Yiisoft\Db\Connection\ConnectionInterface;
use Yiisoft\Db\Mssql\Connection;
use Yiisoft\Db\Mssql\Driver;
use Yiisoft\Profiler\ProfilerInterface;
/** @var array $params */
return [
ConnectionInterface::class => [
'class' => Connection::class,
'__construct()' => [
'driver' => new Driver(
$params['yiisoft/db-mssql']['dsn'],
$params['yiisoft/db-mssql']['username'],
$params['yiisoft/db-mssql']['password'],
),
],
],
];
Create a file config/common/params.php
for common
parameters.
use Yiisoft\Db\Mssql\Dsn;
return [
'yiisoft/db-mssql' => [
'dsn' => (new Dsn('sqlsrv', 'localhost', 'yiitest'))->asString(),
'username' => 'user',
'password' => 'password',
],
];
To configure the connection without DI container, you need to follow these steps:
use Yiisoft\Cache\ArrayCache;
use Yiisoft\Db\Cache\SchemaCache;
use Yiisoft\Db\Mssql\Connection;
use Yiisoft\Db\Mssql\Driver;
use Yiisoft\Db\Mssql\Dsn;
// Dsn.
$dsn = (new Dsn('sqlsrv', 'localhost', 'yiitest'))->asString();
// PSR-16 cache implementation.
$arrayCache = new ArrayCache();
// Schema cache.
$schemaCache = new SchemaCache($cache);
// PDO driver.
$pdoDriver = new Driver($dsn, 'user', 'password');
// Connection.
$db = new Connection($pdoDriver, $schemaCache);