@darion
To connect SQL Server with Yii2, follow these steps:
1
|
composer require --prefer-dist yiisoft/yii2-mssql |
1 2 3 4 5 6 7 |
return [
'class' => 'yiidbConnection',
'dsn' => 'sqlsrv:Server=localhost;Database=mydatabase',
'username' => 'myusername',
'password' => 'mypassword',
'charset' => 'utf8',
];
|
1 2 3 4 |
'components' => [
'db' => require __DIR__ . '/db.php',
// other components...
],
|
1 2 3 4 5 6 7 8 9 10 11 12 |
use yiidbQuery;
public function actionTestConnection()
{
$query = new Query;
$rows = $query->select('*')
->from('mytable')
->limit(10)
->all();
print_r($rows);
}
|
When you access the action URL, it should display the first 10 rows from the 'mytable' table.
That's it! You have successfully connected SQL Server with Yii2.