10

I am trying to use Doctrine 2 (for Symfony 2) to connect to MSSQLServer from a linux machine.

I have installed pdo_dblib (PDO Driver for FreeTDS/Sybase DB-lib) and am able to connect to the db server via tsql on the command line and from the php cli also. Thus I know this is working.

In my Symfony/app/config/parameters.ini file I had specified database_driver="pdo_sqlsrv" as the database driver (as I read that this would be handled by db_lib) but when trying to run a create database command (using the command php app/console doctrine:database:create) I am getting the error:

Could not create database for connection named could not find driver

I then changed the driver to database_driver="pdo_dblib" and I am now getting the error:

[Doctrine\DBAL\DBALException]
The given 'driver' pdo_dblib is unknown, Doctrine currently supports only the following drivers: pdo_mysql, pdo_sqlite, pdo_pgsql, pdo_oci, oci8, ibm_db2, pdo_ibm, pdo_sqlsrv

So it seems that to connect to MSSQL my only option is pdo_sqlsrv, so I went to install this. However, I have just discovered here, that

The PDO_SQLSRV extension is only compatible with PHP running on Windows.

Thus the driver supported by doctrine and those available to use on linux seem to be mutually exlusive. From searching I haven't found any instances of this issue being solved thus far (One guy marked the issue as solved, but when I read the thread he had simply moved his dev env to a windows box... not exactly what I had in mind!).

Manse
  • 37,765
  • 10
  • 83
  • 108
Rob Ganly
  • 403
  • 2
  • 5
  • 14
  • Please take a look here: http://stackoverflow.com/questions/8492941/doctrine-2-how-to-add-custom-dbal-driver and leave a message if the non-accepted answer did help you. – hakre Jun 19 '12 at 04:08
  • Hi Hakre, Thanks for your response. I've read your post, and understand what you are describing, but where did you get this class from? driver_class: Doctrine\DBAL\Driver\MsSql\Driver Did you have to manually copy it from somewhere? As mentioned, I am using pdo_dblib. Rob Ganly – Rob Ganly Jun 19 '12 at 08:59

1 Answers1

8

Under linux (at least Debian based distros), php needs the package php5-sybase that brings support for Sybase and MSSql.

If you are using a debian based distribution you will want to do

$ sudo apt-get install php5-sybase
$ sudo service apache2 restart

And

php -r "phpinfo();" | grep "PDO drivers"

should give you

PDO drivers: dblib, mysql, sqlite, ...

dblib is actually the one we need

Now to be able to use this driver with Doctrine, this post: Doctrine 2 - How to add custom DBAL driver? helped me to find the answer.

The OP suggests to use this bundle on git that makes things work together.

Community
  • 1
  • 1
Pierre de LESPINAY
  • 44,700
  • 57
  • 210
  • 307
  • Thanks Pierre, I found that post you mentioned on adding a custom DBAL driver. I also got to the stage where I was getting the error mentioned in that post, i.e. "The given 'driver' pdo_dblib is unknown". Seeing as it wasn't supported 'out of the box' by doctrine2 I was unsure if there would be problems further down the line if I did this modification, so decided that we didn't *need* to use MSSQL for the project. I used MySQL instead, and this proved unproblematic. It seems like other people have that solution working though, so I'll accept your answer. Thanks again! Rob Ganly – Rob Ganly Jul 12 '12 at 14:31
  • It's surely a better idea to use MySQL or Postgre. I actually had to use MSSQL as a alternate connector to read external data. – Pierre de LESPINAY Jul 18 '12 at 07:07
  • Yes it certainly makes more sense to use MySql with PHP and Doctrine 2/ Symfony 2, this was understood and considered before starting the project. However seeing as it meant going against the status quo of the development and production environments here I decided to puruse the MSSQL route. However when these problems became evident a measured view was taken and this route abandoned. Such is the life of a developer! Works great with MySQL now though! Thanks for your input. :) Rob Ganly – Rob Ganly Jul 19 '12 at 08:15