在 Linux 上为 SYMFONY 2 将 Doctrine 2 连接到 MSSQL

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11081993/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 06:55:36  来源:igfitidea点击:

Connecting Doctrine 2 to MSSQL For SYMFONY 2 On Linux

phpsql-serverlinuxsymfonydoctrine-orm

提问by Rob Ganly

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

我正在尝试使用 Doctrine 2(用于 Symfony 2)从 Linux 机器连接到 MSSQLServer。

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.

我已经安装了 pdo_dblib(FreeTDS/Sybase DB-lib 的 PDO 驱动程序)并且能够通过命令行上的 tsql 和 php cli 连接到数据库服务器。因此我知道这是有效的。

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:

在我的 Symfony/app/config/parameters.ini 文件中,我将 database_driver="pdo_sqlsrv" 指定为数据库驱动程序(正如我所读到的,这将由 处理db_lib)但是在尝试运行创建数据库命令时(使用命令php app/console doctrine:database:create)我我收到错误:

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:

然后我将驱动程序更改为database_driver="pdo_dblib",现在出现错误:

[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

[Doctrine\DBAL\DBALException]
给定的“驱动程序”pdo_dblib 未知,Doctrine 目前仅支持以下驱动程序: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

所以似乎连接到 MSSQL 我唯一的选择是pdo_sqlsrv,所以我去安装它。然而,我刚刚在这里发现

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

PDO_SQLSRV 扩展仅与在 Windows 上运行的 PHP 兼容。

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!).

因此,学说支持的驱动程序和可在 linux 上使用的驱动程序似乎是相互排斥的。通过搜索,到目前为止我还没有发现任何解决此问题的实例(一个人将该问题标记为已解决,但是当我阅读该线程时,他只是将他的开发环境移到了 Windows 框...心里!)。

采纳答案by Pierre de LESPINAY

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

在 linux(至少是基于 Debian 的发行版)下,php 需要php5-sybase支持 Sybase 和 MSSql的包。

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

如果你使用的是基于 debian 的发行版,你会想要做

$ 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, ...

PDO 驱动程序:dblib、mysql、sqlite、...

dblibis actually the one we need

dblib实际上是我们需要的

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.

现在为了能够在 Doctrine 中使用这个驱动程序,这篇文章: Doctrine 2 - 如何添加自定义 DBAL 驱动程序?帮助我找到了答案。

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

OP 建议在 git上使用这个,让事情一起工作。