首页 > 解决方案 > 如何在我的 symfony 项目中将 SQL Server 与 ORM Doctrine 连接起来?

问题描述

doctrine:
    dbal:
        # configure these for your database server
        driver: 'pdo_mysql'
        server_version: '5.7'
        charset: utf8mb4
        host: 127.0.0.1
        port: 8889
        user: '%env(DATABASE_USER)%'
        password: '%env(DATABASE_PWD)%'
        dbname: '%env(DATABASE_NAME)%'

        # With Symfony 3.3, remove the `resolve:` prefix
     #   url: '%env(resolve:DATABASE_URL)%'
    orm:
        auto_generate_proxy_classes: '%kernel.debug%'
        naming_strategy: doctrine.orm.naming_strategy.underscore
        auto_mapping: true
        mappings:
            App:
                is_bundle: false
                type: yml
                dir: '%kernel.project_dir%/config/doctrine'
                prefix: 'App\Entity'
                alias: App

标签: sql-serversymfonyormdoctrine

解决方案


您需要使用驱动程序pdo_sqlsrvsqlsrv,而不是pdo_mysql

参见学说配置文档)。

pdo_sqlsrv:使用 pdo_sqlsrv PDO 的 Microsoft SQL Server 驱动程序

sqlsrv:使用 sqlsrv PHP 扩展的 Microsoft SQL Server 驱动程序。

pdo_sqlsrv/的可用配置sqlsrv是:

user (string): Username to use when connecting to the database.
password (string): Password to use when connecting to the database.
host (string): Hostname of the database to connect to.
port (integer): Port of the database to connect to.
dbname (string): Name of the database/schema to connect to.

文档说:

以下驱动程序支持开箱即用的自动数据库平台检测,无需任何额外配置:

pdo_mysql
mysqli
pdo_pgsql
pdo_sqlsrv
sqlsrv

所以你不需要指定server_version.

你将会拥有:

doctrine:
    dbal:
        # configure these for your database server
        driver: 'pdo_sqlsrv'
        charset: utf8mb4
        host: 127.0.0.1
        port: 8889
        user: '%env(DATABASE_USER)%'
        password: '%env(DATABASE_PWD)%'
        dbname: '%env(DATABASE_NAME)%'

        # With Symfony 3.3, remove the `resolve:` prefix
     #   url: '%env(resolve:DATABASE_URL)%'
    orm:
        auto_generate_proxy_classes: '%kernel.debug%'
        naming_strategy: doctrine.orm.naming_strategy.underscore
        auto_mapping: true
        mappings:
            App:
                is_bundle: false
                type: yml
                dir: '%kernel.project_dir%/config/doctrine'
                prefix: 'App\Entity'
                alias: App

推荐阅读