Created
November 20, 2013 11:24
-
-
Save sandvige/7561636 to your computer and use it in GitHub Desktop.
MSSQL on Linux using DBLib
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace RG\CoreBundle\Driver\PDODblib; | |
use Doctrine\DBAL\Driver\PDOConnection; | |
use Doctrine\DBAL\Driver\Connection as BaseConnection; | |
class Connection extends PDOConnection implements BaseConnection | |
{ | |
public function lastInsertId($name = null) | |
{ | |
$stmt = $this->query('SELECT SCOPE_IDENTITY()'); | |
$id = $stmt->fetchColumn(); | |
$stmt->closeCursor(); | |
return $id; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace RG\CoreBundle\Driver\PDODblib; | |
use Doctrine\DBAL\Platforms\SQLServer2008Platform; | |
use Doctrine\DBAL\Schema\SQLServerSchemaManager; | |
use Doctrine\DBAL\Driver as BaseDriver; | |
use Doctrine\DBAL\Connection as BaseConnection; | |
use Doctrine\DBAL\Types\Type; | |
class Driver implements BaseDriver | |
{ | |
public function __construct() | |
{ | |
Type::overrideType('datetime', 'RG\CoreBundle\Driver\PDODblib\Type\DateTimeType'); | |
} | |
public function connect(array $params, $username = null, $password = null, array $driverOptions = array()) | |
{ | |
return new Connection( | |
$this->_constructPdoDsn($params), | |
$username, | |
$password, | |
$driverOptions | |
); | |
} | |
private function _constructPdoDsn(array $params) | |
{ | |
$dsn = 'dblib:'; | |
if (isset($params['host'])) | |
$dsn .= 'host=' . $params['host'] . ';'; | |
if (isset($params['port'])) | |
$dsn .= 'port=' . $params['port'] . ';'; | |
if (isset($params['dbname'])) | |
$dsn .= 'dbname=' . $params['dbname'] . ';'; | |
return $dsn; | |
} | |
public function getDatabasePlatform() | |
{ | |
return new SQLServer2008Platform(); | |
} | |
public function getSchemaManager(BaseConnection $conn) | |
{ | |
return new SQLServerSchemaManager($conn); | |
} | |
public function getName() | |
{ | |
return 'pdo_dblib'; | |
} | |
public function getDatabase(BaseConnection $conn) | |
{ | |
$params = $conn->getParams(); | |
return $params['dbname']; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace RG\CoreBundle\Driver\PDODblib\Type; | |
use Doctrine\DBAL\Platforms\AbstractPlatform; | |
use Doctrine\DBAL\Types\DateTimeType as BaseDateTimeType; | |
use Doctrine\DBAL\Types\ConversionException; | |
use DateTime; | |
class DateTimeType extends BaseDateTimeType | |
{ | |
public function convertToPHPValue($value, AbstractPlatform $platform) | |
{ | |
if (!$value) | |
return null; | |
$timestamp = strtotime($value); | |
if($timestamp === false) { | |
$val = DateTime::createFromFormat($platform->getDateTimeFormatString(), $value); | |
if (!$val) | |
throw ConversionException::conversionFailedFormat($value, $this->getName(), $platform->getDateTimeFormatString()); | |
} | |
$val = new DateTime(); | |
$val->setTimestamp($timestamp); | |
return $val; | |
} | |
public function convertToDatabaseValue($value, AbstractPlatform $platform) | |
{ | |
return ($value !== null) | |
? $value->format('Y-m-d H:i:s' . '.000') : null; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Doctrine Configuration | |
doctrine: | |
dbal: | |
driver: %database_driver% | |
driver_class: %database_driver_class% | |
host: %database_host% | |
port: %database_port% | |
dbname: %database_name% | |
user: %database_user% | |
password: %database_password% | |
charset: UTF8 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
parameters: | |
# On Linux | |
# database_driver: ~ | |
# database_driver_class: \RG\CoreBundle\Driver\PDODblib\Driver | |
# On Windows | |
# database_driver: pdo_sqlsrv | |
# database_driver_class: ~ | |
database_driver: ~ | |
database_driver_class: ~ | |
... more and more |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment