Microsoftprovidesa? SQLServer? Driver? ForPDO. Unfortunately, thisdriveronlyworksonWindows. linuxandmacosx?mustusethe? FreeTDS? Compatibilitylayer :? AnopensourceimplementationoftheMSSQLServerprotocolforUnix. It's
Microsoft provides? SQL Server? Driver? For PDO. Unfortunately, this driver only works on Windows. Linux and Mac OS X apps must use? FreeTDS? Compatibility layer :? An open source implementation of the ms SQL Server protocol for Unix. It's
Microsoft provides? SQL Server? Driver? For PDO. Unfortunately, this driver only works on Windows. Linux and Mac OS X apps must use? FreeTDS? Compatibility layer :? An open source implementation of the ms SQL Server protocol for Unix.
It's possible to connect? A Symfony app to a SQL Server instance on Unix through FreeTDS but this involve to use a Doctrine driver that is not provided with the standard distribution. some tutorials already explain how to do that, but they encourage doing dirty things like editing files in?vendor/
? Directory. Here is the clean way!
First, install FreeTDS.
On Mac ox x, use? Homebrew:
brew install freetds
On Debian or Ubuntu:
apt-get install freetds-bin
The next step is to configure FreeTDS to be able to connect to the SQL Server instance.
Open?freetds.conf
? File (/etc/freetds/freetds.conf
? On Debian/Ubuntu and/usr/local/etc/freetds.conf
? On Mac OS X) and add the connection details of your server:
[my_server]
host = sql.example.com
port = 1433
tds version = 8.0
client charset = UTF-8
text size = 20971520
Be sure to set the protocol version to 8.0, the client charset and the text size.
Now, you should be able to connect to the SQL server from the command line:
tsql -S my_server -U myusername
Type your password when asked and?Ctrl+D
? To disconnect from the server.
It's time to install? Dblib pdo Driver.
On Mac OS X (replace php55 by the version of PHP you are using ):
brew install?php55-pdo-dblib
On Debian or Ubuntu:
apt-get install php5-sybase
And add the DBLIB driver for Doctrine (packaged in? A Symfony bundle) in your app:
# in your Symfony app directory
composer require?realestateconz/mssql-bundle:dev-master
Enable the Symfony bundle. Add this line in?registerBundles()
? Method of your?AppKernel
In?app/AppKernel.php
:
New Realestate \ MssqlBundle \ RealestateMssqlBundle (), |
Finally, configure Doctrine to use this driver. Edit?app/config/config.yml
:
Doctrine: ???? Dbal: ???????? Driver_class: Realestate \ MssqlBundle \ Driver \ PDODblib \ Driver ???????? Host: my_server ???????? Dbname: MYDATABASE ???????? User: myuser ???????? Password: mypassword |
Note that you must use?driver_class
? Parameter, and not?driver
. Of course, you shocould not hardcode these values. Use? The interactive parameters system? Instead.
Your Symfony app is now able to connect to the SQL Server. Try to run a SQL query:
php app/console doctrine:query:sql "SELECT * FROM MY_TABLE"
As SQL Server is a bad default DBMS for a Symfony app, especially on Unix servers, you shoshould be interested? Using multiple database connection with Symfony and Doctrine.
In addition, pay attention to your encoding. sqlserver insert needs to convert your utf8 to gb2312.
Like this:
$ Data = array_map (function ($ value) {return iconv ("UTF-8", "GB2312", $ value) ;}, $ data );
Original article address: On linux or mac, how does symfony connect to sqlserver? Thanks to the original author for sharing.