If you’re reading this, you’re probably at the point where you’ve got your Symfony project setup and your routing / controllers / views are ready to go. Now you’re looking to add your first form, and you want it to interact with tables in your database.
This is part one of a two-part walkthrough, where we will be adding Users to our database and sorting them into Groups, using a form in our application. If you’d like to follow along, please use the following file to create the database tables used in this example: db_users_and_groups.sql
We’ll begin with how to map our entities. First off, it is important to think of Entities in the singular form. An Entity represents a single row in your table. If you have a table named “Groups”, your Entity used to represent it would be named “Group”.
This example will not be walking through the specifics of mapping each property, but rather showing the result of a mapped table and pointing out important information. The annotations are very straight forward, so let’s jump in…
Here is our Users table structure:
Here is how the
User Entity looks when finished.
View file on [Github][entity_user] [entity_user]: http://github.com/kurtfunai/WalkthroughBundle/blob/master/Entity/User.php
######Things to note:
That Doctrine2 Entities are mapped through Docblock Annotations.
We declare that it is an entity, and the associated table name, before the class.
If you’re looking for information on how to map different data types, it can be found here
That we allow our $middleName property to be nullable.
That we’re using $group to represent our Foreign Key relationship to the Groups table.
Now, you’ve probably got some questions about how this is setup, and how we decided to use the ManyToOne annotation. For the sake of simplicity, we are mapping a OneToMany relationship - one group to many users. Because of this, our
User Entity is on the Many side of the relationship. With Doctrine2, in a OneToMany relationship, the “Many” is always the owning side. If you’re looking for more information on how this is configured, or about ManyToMany relationships, it can be found here.
In this case,
$group will represent an entire
Group Entity. We are specifying the targeted entity
kurtfunai\WalkthroughBundle\Entity\Group, and the property that will be the inverse side of the relationship -
We still haven’t actually created our Group Entity yet so…
This is how our Groups table is defined in the database:
Now let’s take a look at how the
Group Entity looks when finished.
View file on Github
######Things to note about how this is mapped:
__construct()for this class, we’re assigning
$membersto be a Doctrine Collection. This will be a collection of the Users in the Group.
__toString()to return the
$this->name;. We’ll get to see the reason why in ‘Part 2 - Forms and Validation’.
$membersto map the inverse side of our relationship with the
User Entity, we mapped the ManyToOne side. Now we’ve mapped our OneToMany side, specifying the targeted entity,
kurtfunai\WalkthroughBundle\Entity\User and the property on the
User Entity that maps the relationship.
You may be wondering why we have added the
$members property when we’ve already mapped the Foreign Key relationship on our
User Entity. With Doctrine2, you can map both sides of the relationship, allowing us to grab all Users belonging to a Group. While this is not necessary, it is useful to know.
That’s it, our Entities for this example are mapped! Now we’ve got to use them to build our Forms, and place information into the database.