Working with Entities in Symfony2

→ Are you a new visitor? Please visit the page guidance for new visitors ←

Working with Entities in Symfony2

Symfony is a very powerfull PHP framework that helps you do amazing projects in small amount of time. It has built in support for Doctrine which is an incredible tool for interacting with databases, not through tables but through php objects. First let’s see what is an ORM and after that we will clarify our subject.

What is ORM?

Object relational mapper (ORM) for PHP that sits on top of a powerful database abstraction layer (DBAL). One of its key features is the option to write database queries in a proprietary object oriented SQL dialect called Doctrine Query Language (DQL). This is what Doctrine is. Basically it allows you to query the database without any knowledge of sql or other languages. You just work with php objects (methods and properties of a certain class).

Our problem

Suppose you’re building an application where some users need to be displayed. Without even thinking about Doctrine or databases, you already know that you need a Users object to represent those products. Create this class inside the Entity directory of your symfony bundle:

First thing that we did above, it was to create a file with the name of our entity (Users.php), located in /Entity directory of our bundle. Inside it, we specified the namespace, who should look like the directory structure that he belongs. The next step was to create a class with the exact name of our file (Users), and after that we added the properties of this class all protected. These properties are exactly the columns from within the “users” table in our database. Now you should see why it is called object relationship mapping more clearly. We setup our class and we are ready to go further. Now we have to tie this object (class) with the “users” table from our database. We can do that from within the same file with annotations (comments that are above each propery and method) or in separate files (yaml files).  For the sake of this tutorial, we will separate them in another file, because it will be more clear for you to understand this relationship between the object and the table from database. Create a folder called “doctrine” in ” src/Bundle/MyBundle/Resources/config/”. In it, you create a file called “Users.orm.yml” with the following contents:

As you can see, we defined the rules for our object (entity) in order to interact with the database table called “users”. We specified the namespace, told doctrine that this is an entity that is tied with the “users” table and after that we specified the exact same fields that we have in our database, starting with the primary key “id”. The rest of the fields are “name”, “email” and “password”. For each field we have a “type” and a “scale”. The “type” is telling us the type of the column that we have in the database. You can see here more of doctrine mapping types. The “scale” tell us about the length of the database column. Great! now we have the database specifications and our object. The only thing that remains is to generate the setters and getters for this entity. By running this into command line (after you navigated to the path of your project), you let Symfony to write in your /Entity/Users.php file:

Heads up! Beware because you can run into this kind of problems!
Now the “Users” Entity should contain methods like these:

With these methods you can interact with the database. You can find more about Doctrine Project here. If you have further questions, please, don’t hesitate to contact us throug comments, or email or any other way.

Request an article ←