Symfony2 ou Silex + Doctrine + Linux connection MSSQL

Doctrine ne permet pas à  l’heure actuelle de se connecter à  un serveur Microsoft SQL server. Voici une astuce pour le faire.

Réf : basé sur https://github.com/trooney/PDODblibBundle

Sur linux

– Installer sybase

apt-get install php5-sybase
/etc/init.d/apache2 restart


– Installer le driver dans symfony2

[…]/vendor/doctrine/dbal/lib/Doctrine/DBAL/DriverManager.php


private static $_driverMap = array(
    'pdo_mysql'  => 'Doctrine\DBAL\Driver\PDOMySql\Driver',
    'pdo_sqlite' => 'Doctrine\DBAL\Driver\PDOSqlite\Driver',
    'pdo_pgsql'  => 'Doctrine\DBAL\Driver\PDOPgSql\Driver',
    'pdo_dblib'  => 'Doctrine\DBAL\Driver\PDODblib\Driver',
    'pdo_oci' => 'Doctrine\DBAL\Driver\PDOOracle\Driver',
    'oci8' => 'Doctrine\DBAL\Driver\OCI8\Driver',
    'ibm_db2' => 'Doctrine\DBAL\Driver\IBMDB2\DB2Driver',
    'pdo_ibm' => 'Doctrine\DBAL\Driver\PDOIbm\Driver',
    'pdo_sqlsrv' => 'Doctrine\DBAL\Driver\PDOSqlsrv\Driver',
    'mysqli' => 'Doctrine\DBAL\Driver\Mysqli\Driver',
    'drizzle_pdo_mysql'  => 'Doctrine\DBAL\Driver\DrizzlePDOMySql\Driver',
    'sqlsrv' => 'Doctrine\DBAL\Driver\SQLSrv\Driver',
);


– Copier le dossier driver PDODblib dans le dossier

[…]/vendor/doctrine/dbal/lib/Doctrine/DBAL/Driver


Téléchargez   le dossier  PDODblib.tar

– Définition de la config dans config.yml

doctrine:
    dbal:
        default_connection: default # précise la connexion utilisée par défaut
        connections:
    default:
                    driver:  pdo_dblib
                    host:     %database_host_mssql%
                    port:     %database_port_mssql%
                    dbname:   %database_name_mssql%
                    user:     %database_user_mssql%
                    password: %database_password_mssql%
                    charset:  %database_charset_mssql%



Adapter pour SQL Server 2008 Platform

Dans Doctrine\DBAL\Driver\PDODblib\Driver.php

Modifier la fonction getDatabasePlatform

//REMPLACER LA FONCTION CI-DESSOUS

public function getDatabasePlatform() {
    	if (class_exists('\\Doctrine\\DBAL\\Platforms\\SQLServer2005Platform')) {
			return new \Doctrine\DBAL\Platforms\SQLServer2005Platform();
		}

		if (class_exists('\\Doctrine\\DBAL\\Platforms\\MsSqlPlatform')) {
			return new \Doctrine\DBAL\Platforms\MsSqlPlatform();
		}
	}

// PAR CELLE-CI

public function getDatabasePlatform() {
    	if (class_exists('\\Doctrine\\DBAL\\Platforms\\SQLServer2008Platform')) {
			return new \Doctrine\DBAL\Platforms\SQLServer2008Platform();
		}
		if (class_exists('\\Doctrine\\DBAL\\Platforms\\SQLServer2005Platform')) {
			return new \Doctrine\DBAL\Platforms\SQLServer2005Platform();
		}

		if (class_exists('\\Doctrine\\DBAL\\Platforms\\MsSqlPlatform')) {
			return new \Doctrine\DBAL\Platforms\MsSqlPlatform();
		}
	}


Ajouter la gestion du type MSSQL Datetime2

Dans Doctrine\DBAL\Types\Type.php
Ajouter la constante : const DATETIME2 = ‘datetime2’;
Modifier la variable static : private static $_typesMap
Comme suit :

// [...]
abstract class Type
{
    const TARRAY = 'array';
    const SIMPLE_ARRAY = 'simple_array';
    const JSON_ARRAY = 'json_array';
    const BIGINT = 'bigint';
    const BOOLEAN = 'boolean';
    const DATETIME = 'datetime';
    const DATETIME2 = 'datetime2';  // <= AJOUTER CETTE LIGNE
    const DATETIMETZ = 'datetimetz';
    const DATE = 'date';
    const TIME = 'time';
    const DECIMAL = 'decimal';
    const INTEGER = 'integer';
    const OBJECT = 'object';
    const SMALLINT = 'smallint';
    const STRING = 'string';
    const TEXT = 'text';
    const BLOB = 'blob';
    const FLOAT = 'float';
    const GUID = 'guid';

    /** Map of already instantiated type objects. One instance per type (flyweight). */
    private static $_typeObjects = array();

    /** The map of supported doctrine mapping types. */
    private static $_typesMap = array(
        self::TARRAY => 'Doctrine\DBAL\Types\ArrayType',
        self::SIMPLE_ARRAY => 'Doctrine\DBAL\Types\SimpleArrayType',
        self::JSON_ARRAY => 'Doctrine\DBAL\Types\JsonArrayType',
        self::OBJECT => 'Doctrine\DBAL\Types\ObjectType',
        self::BOOLEAN => 'Doctrine\DBAL\Types\BooleanType',
        self::INTEGER => 'Doctrine\DBAL\Types\IntegerType',
        self::SMALLINT => 'Doctrine\DBAL\Types\SmallIntType',
        self::BIGINT => 'Doctrine\DBAL\Types\BigIntType',
        self::STRING => 'Doctrine\DBAL\Types\StringType',
        self::TEXT => 'Doctrine\DBAL\Types\TextType',
        self::DATETIME => 'Doctrine\DBAL\Types\DateTimeType',
        self::DATETIME2 => 'Doctrine\DBAL\Types\DateTimeType',  // <= AJOUTER CETTE LIGNE
        self::DATETIMETZ => 'Doctrine\DBAL\Types\DateTimeTzType',
        self::DATE => 'Doctrine\DBAL\Types\DateType',
        self::TIME => 'Doctrine\DBAL\Types\TimeType',
        self::DECIMAL => 'Doctrine\DBAL\Types\DecimalType',
        self::FLOAT => 'Doctrine\DBAL\Types\FloatType',
        self::BLOB => 'Doctrine\DBAL\Types\BlobType',
        self::GUID => 'Doctrine\DBAL\Types\GuidType',
    );
// [...]