Reading From SQL Server with Symfony

After you have installed Symfony and configured the database connection, you will want to write some code to retrieve data from the database.

Install Doctrine

The first step involves installing the Doctrine library to map your database. To do so, open a command prompt and navigate to your project directory. Then enter the following installation command:


composer require doctrine/doctrine-bundle


You should eventually get a message saying all assets were successfully installed.

Finally, enable the bundle by adding the following to the $bundles array inside the registerBundles function in the app/AppKernel.php file:


new Doctrine\Bundle\DoctrineBundle\DoctrineBundle(),


Create a Class for Your Data

Next you will need to create a class (aka entity) to map and hold the data from your database.

Create an Entity folder inside src/AppBundle. Within that folder create a class file (ex. Employee.php) with the following code:


namespace AppBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

* @ORM\Entity
* @ORM\Table(name=”tablelNameGoesHere”)

class Employee {
* @ORM\Column(type=”integer”)
private $ID;

* @ORM\Column(type=”nvarchar”, length=255)
private $FIRST_NAME;

* @ORM\Column(type=”nvarchar”, length=255)
private $LAST_NAME;


Note that you will want to change your field names and data types to match the database you are using.

Also note that instead of using the PHP annotations above, you could use YAML or XML as explained in this Symfony Doctrine page.

In a subsequent post, I will describe how to read and display data.

Connecting Symfony to SQL Server

This is the second of my posts documenting how I configured the PHP Symfony framework for Windows. For work, I needed to connect my Symfony installation to SQL Server. There are two steps to do so: install the driver and update the configuration settings.

First, download the Sql Server driver:

Then copy the appropriate dll to your PHP ext folder.

This page will tell you which dll to choose:

Next, add the dll file name to your php.ini config file, like this:


Then restart the web server.

Finally, you need to update the Symfony config. In your app/config folder change the driver under the doctrine section to:


For security reasons, your other configuration settings will likely already be set to variables. Those variables can be found in parameters.yml.

In the parameters config, update any connection settings as necessary to match the setup of your database.

Basic Symfony Installation for Windows

I recently had to install Symfony, the PHP component-based framework, for a work project. There is extensive documentation on the Symfony site, however I thought I would post a quick start guide with the essential steps for getting Symfony up and running quickly on a Windows server environment.

Why Symfony?

I chose Symfony because it was the least framework-like method for structuring a PHP application. In reading Josh Lockhart’s ‘Modern PHP’ this past year, he notes that the problem in choosing a traditional framework is that you are committing to that framework’s future. Additionally, frameworks have a wide variety of tools, but sometimes you just need something specific, which the framework may not provide. Symfony, on the other hand, is a collection of decoupled components that can be used in isolation or as part of a framework.

Installation Steps

The easiest way that I found to install Symfony is with Composer, the dependency management tool for PHP.

There are three steps: install Composer, create a Symfony project, and start the server.

1. Go to the Composer site and download and run the application via the executable.

2. Open a Windows command prompt and enter the following command (change ‘project_name’ to whatever you want to call your project):

composer create-project symfony/framework-standard-edition project_name

Composer will then execute a series of commands. At some point it will ask you for database credentials. You can hit enter for each one to leave them at their defaults initially. Of course, before moving to a production environment you will want to change these.

Eventually, you will get a message saying all assets were successfully installed.

3. Finally, navigate to your directory and start the server with the following two commands (for ‘project_name’ enter whatever you named your project in step two):

cd project_name/

php bin/console server:run

Symfony should now be installed. You can verify this by pulling up http://localhost:8000/ in your browser, where you will get a Symfony welcome message.