Blog Post • development

Querying a MSSQL database from Symfony

October 4, 2018by Simon Westyn 3 min read
Blog Post • development
Querying a MSSQL database from Symfony
Back to top

For a recent project, we had to connect and query a Microsoft SQL database from a Symfony project. Since we use Docker to run the apps in separate containers, we added a new container to simulate the MSSQL database.

We added the following code to our docker-compose.yml file in the root of our Symfony project:

version: '2'

    image: 'microsoft/mssql-server-linux:latest'
      - SA_PASSWORD=<password>
      - '1433:1433'
      - mssqldata:/var/opt/mssql/data

    build: ./mssql-setup
    image: 'activelamp/iamucla-mssql-setup'
      - SA_PASSWORD=<password>
      - mssql:api-mssql
  mssqldata: {}

In the mssql-setup folder in the root of our Symfony project, we have another Dockerfile with the following contents to set up the database:

FROM ubuntu:16.04

# Install update instance
RUN apt-get -y update \
    && apt-get install curl -y \
    && apt-get install apt-transport-https \
    && apt-get install -y locales \
    && echo "en_US.UTF-8 UTF-8" > /etc/locale.gen \
    && locale-gen \
    && curl | apt-key add - \
    && curl | tee /etc/apt/sources.list.d/msprod.list \
    && apt-get -y update \
    && ACCEPT_EULA=Y apt-get install -y mssql-tools unixodbc-dev

# Create sqlsetup directory
RUN mkdir -p /usr/src/sqlsetup
WORKDIR /usr/src/sqlsetup

# Bundle sqlsetup source
COPY . /usr/src/sqlsetup

# Grant permissions for the import-data script to be executable
RUN chmod +x /usr/src/sqlsetup/

CMD /bin/bash ./

As you can see in the above code, we execute a script called . This imports the setup.sql file for creating the needed tables (both files are also located in the mssql-setup folder):

#wait for the SQL Server to come up
sleep 30s

#run the setup script to create the DB and the schema in the DB
/opt/mssql-tools/bin/sqlcmd -S api-mssql -U sa -P $SA_PASSWORD -d master -i setup.sql

In the beginning of this project, we started off using the LeaseWeb DoctrinePdoDblib driver but quickly noticed that this driver wasn't returning the expected error messages coming from MSSQL and it also did not return any message strings returned from custom Stored Procedures. That's when we made the switch to use the official Microsoft SQL drivers for PHP.

But this required some changes to our root Dockerfile so we could install these drivers in our Symfony container.

FROM php:7.1.16-apache

RUN curl -sS | php \
  && mv composer.phar /usr/local/bin/composer \
  && chmod ugo+x /usr/local/bin/composer


RUN apt-get update && apt-get install -y \
  vim \
  git \
  unzip \
  wget \
  curl \
  libmcrypt-dev \
  libcurl4-openssl-dev \
  mysql-client \
  nodejs \
  libxml2-dev \
  libldb-dev libldap2-dev \
  build-essential \
  freetds-bin \
  freetds-dev \

#Microsoft Drivers for PHP for SQL Server:
RUN curl | apt-key add - \
  && curl > /etc/apt/sources.list.d/mssql-release.list \
  && apt-get install -y locales \
  && echo "en_US.UTF-8 UTF-8" > /etc/locale.gen \
  && locale-gen

RUN apt-get -y update
RUN ACCEPT_EULA=Y apt-get install -y msodbcsql
RUN apt-get install -y unixodbc-dev

RUN pear config-set php_ini `php --ini | grep "Loaded Configuration" | sed -e "s|.*:\s*||"` system
RUN pecl install sqlsrv
RUN pecl install pdo_sqlsrv

RUN echo "\" > /usr/local/etc/php/conf.d/symfony.ini


As you can see in the above snippet, we had to install the sqlsrv and pdo_sqlsrv drivers and enable these extensions in our symfony.ini file.

The next step was to define the connection in our Symfony config.yml file as follows:

# Doctrine Configuration
        driver: pdo_sqlsrv
        host: '%mssql_database_host%'
        port: '%mssql_database_port%'
        dbname: '%mssql_database_name%'
        user: '%mssql_database_user%'
        password: '%mssql_database_password%'
        charset: UTF8
        wrapper_class: AppBundle\Connections\ConnectionMSSQL

As you can see, we defined a wrapper_class in our connection, this ConnectionMSSQL class extends the Doctrine\DBAL\Connection. In this ConnectionMSSQL class, you can write all your functions to query the DB, e.g.:

public function findUserById($id)
    $sql = 'SELECT * FROM my_users
         WHERE UPPER(id) = UPPER(:id)';
    return $this->fetchAll(
        $sql, [
            'id' => strtoupper($id),

Authored by