Installation
Add the bundle to your composer.json:
composer require isoft/mssql-bundle
Enable it in config/bundles.php:
Intellectsoft\MssqlBundle\IntellectsoftMssqlBundle::class => ['all' => true],
Configuration
Update config/packages/intellectsoft_mssql.yaml (or create it):
intellectsoft_mssql:
driver: "pdo_sqlsrv"
host: "your_host"
port: "1433"
dbname: "your_db"
user: "your_user"
password: "your_password"
charset: "utf8"
options:
PDO::ATTR_PERSISTENT => true
First Use Case
Inject the Doctrine\DBAL\Connection service into a controller or service:
use Doctrine\DBAL\Connection;
class MyController extends AbstractController
{
public function __construct(private Connection $connection)
{
}
public function index()
{
$query = $this->connection->query('SELECT * FROM users');
$users = $query->fetchAllAssociative();
return $this->json($users);
}
}
Entity Manager Configuration
Extend Intellectsoft\MssqlBundle\DependencyInjection\Compiler\DoctrineCompilerPass to configure Doctrine for MSSQL:
# config/packages/doctrine.yaml
doctrine:
dbal:
driver: "pdo_sqlsrv"
url: "%env(DATABASE_URL)%"
server_version: "12.0"
charset: "utf8"
default_table_options:
charset: utf8mb4
collate: utf8mb4_unicode_ci
Schema Management
Use doctrine:schema:update with --complete flag for MSSQL-specific schema generation:
php bin/console doctrine:schema:update --complete --force
QueryBuilder Patterns Leverage MSSQL-specific functions in QueryBuilder:
$queryBuilder->select('CONVERT(varchar, created_at, 120) as formatted_date');
Batch Processing
Use executeStatement for bulk operations:
$this->connection->executeStatement(
'UPDATE users SET last_login = CURRENT_TIMESTAMP WHERE id IN (:ids)',
['ids' => [1, 2, 3]]
);
Stored Procedures Call MSSQL stored procedures directly:
$stmt = $this->connection->prepare('EXEC sp_get_users @type = :type');
$stmt->execute(['type' => 'active']);
$users = $stmt->fetchAllAssociative();
connection.connect and connection.disconnect events for logging/retries:
$connection->getEventManager()->addListener(
ConnectionEvents::CONNECT,
function (ConnectEventArgs $event) {
// Pre-connection logic
}
);
Driver-Specific Quirks
COLLATE for case-sensitive queries:
SELECT * FROM users WHERE username COLLATE SQL_Latin1_General_CP1_CS_AS = 'Admin'
CONVERT or FORMAT for consistent date strings:
SELECT FORMAT(created_at, 'yyyy-MM-dd HH:mm:ss') as formatted_date
Connection Pooling
options:
PDO::ATTR_PERSISTENT => true
doctrine:query-sql:
php bin/console doctrine:query-sql
Character Encoding
SELECT N'Your Unicode String' as text
charset: "utf8mb4"
Query Logging
Enable SQL logging in config/packages/monolog.yaml:
handlers:
doctrine:
type: stream
path: "%kernel.logs_dir%/%kernel.environment%.doctrine.log"
level: debug
channels: ["doctrine"]
Error Handling
Catch PDOException for MSSQL-specific errors:
try {
$this->connection->executeQuery('SELECT * FROM nonexistent_table');
} catch (PDOException $e) {
if ($e->getCode() === '42S02') { // Table not found
// Handle gracefully
}
}
Custom Types
Register MSSQL-specific Doctrine types (e.g., sql_variant):
use Doctrine\DBAL\Types\Types;
Types::addType('sql_variant', SqlVariantType::class);
Schema Filters
Override Intellectsoft\MssqlBundle\Doctrine\MssqlSchemaManager to customize schema generation:
class CustomSchemaManager extends MssqlSchemaManager
{
public function createTableSql(Table $table)
{
// Add MSSQL-specific constraints (e.g., FILESTREAM)
$sql = parent::createTableSql($table);
return $sql . " WITH (FILESTREAM_ON = N'MyContainer')";
}
}
Event Subscribers
Extend Intellectsoft\MssqlBundle\EventListener\MssqlListener for custom logic:
class CustomMssqlListener extends MssqlListener
{
public function postLoad(PostLoadEventArgs $event)
{
// Transform MSSQL-specific data types
}
}
How can I help you explore Laravel packages today?