Revision #231 has been created by rackycz on Aug 26, 2020, 8:16:56 PM with the memo:
mssql
« previous (#230) next (#232) »
Changes
Title
unchanged
Yii v2 snippet guide
Category
unchanged
Tutorials
Yii version
unchanged
2.0
Tags
unchanged
tutorial,beginner,yii2
Content
changed
[...]
// ...
return implode(";", $result);
}
```
**Connection to MSSQL**
---
You might need MSSQL drivers in PHP. Based on your system you have to download different driver. The differences are x64 vs x86 and ThreadSafe vs nonThreadSafe. In Windows I always use ThreadSafe.
[Explanation](https://www.php.net/manual/en/faq.obtaining.php#faq.obtaining.threadsafety).
Newest PHP drivers are [here](https://docs.microsoft.com/en-us/sql/connect/php/download-drivers-php-sql-server).
- Drivers v5.8 = PHP 7.2 - 7.4
Older PHP drivers [here](https://www.microsoft.com/en-us/download/details.aspx?id=20098).
- Drivers v4.0 = PHP 7.0 - 7.1
- Drivers v3.2 = PHP 5.x
Once drivers are downloaded and extracted, pick one DLL file and place it into folder "php/ext". On Windows it might be for example here: "C:\xampp\php\ext"
**Note:**
In some situations you could also need [these OBDC drivers](https://docs.microsoft.com/en-us/sql/connect/odbc/download-odbc-driver-for-sql-server), but I am not sure when:
Now file **php.ini** must be modified. On Windows it might be placed here: "C:\xampp\php\php.ini". Open it and search for rows starting with word "extension" and paste there cca this:
```php
extension={filename.dll}
// Example:
extension=php_pdo_sqlsrv_74_ts_x64.dll
```
Now restart Apache and visit phpinfo() web page. You should see section "pdo_sqlsrv". If you are using XAMPP, it might be on this URL: [http://localhost/dashboard/phpinfo.php](http://localhost/dashboard/phpinfo.php).
Then just add connection to your MSSQL DB in Yii2 config. In my case the database was remote so I needed to create 2nd DB connection. Read next chapter how to do it.
**Using MSSQL database as the 2nd DB in the Yii2 project**
---
Adding 2nd database is done like this in yii-config:
```php
'db' => $db, // the original DB
'db2'=>[
'class' => 'yii\db\Connection',
'driverName' => 'sqlsrv',
// I didnt need to specify database like this:
// 'dsn' => 'sqlsrv:Server={serverName};Database={dbName}',
'dsn' => 'sqlsrv:Server={serverName}',
'username' => '{username}',
'password' => '{pwd}',
'charset' => 'utf8',
],
```
That's it. Now you can test your DB like this:
```php
$result = Yii::$app->db2->createCommand('SELECT * FROM {tblname}')->queryAll();
var_dump($result);
```
Note that in MSSQL you can have longer table names. Example: CATEGORY.SCHEMA.TBL_NAME
And your first test-model can look like this (file MyMsModel.php):
```php
namespace app\models;
use Yii;
use yii\helpers\ArrayHelper;
class MyMsModel extends \yii\db\ActiveRecord
{
public static function getDb()
{
return \Yii::$app->db2;
}
public static function tableName()
{
return 'CATEGORY.SCHEMA.TBL_NAME';
}
}
```
Usage:
```php
$result = MyMsModel::find()->limit(2)->all();
var_dump($result);
```