Working with Entities in Symfony2
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
:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
<?php // src/Bundle/MyBundle/Entity/Users.php namespace Bundle\MyBundle\Entity; class Users { protected $id; protected $name; protected $email; protected $password; } ?> |
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
# src/Bundle/MyBundle/Resources/config/doctrine/Users.orm.yml Bundle\MyBundle\Entity\Users: type: entity table: users id: id: type: integer generator: { strategy: AUTO } fields: name: type: string length: 100 email: type: string scale: 100 password: type: string scale: 255 |
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:
1 |
php app/console doctrine:generate:entities Acme/StoreBundle/Entity/Product |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
<?php // src/Bundle/MyBundle/Entity/Users.php namespace Bundle\MyBundle\Entity; class Users { protected $id; protected $name; protected $email; protected $password; /** * Get id * * @return integer */ public function getId() { return $this->id; } /** * Set email * * @param string $email * @return Users */ public function setEmail($email) { $this->email = $email; return $this; } /** * Get email * * @return string */ public function getEmail() { return $this->email; } /** * Set password * * @param string $password * @return Users */ public function setPassword($password) { $this->password = $password; return $this; } /** * Get password * * @return string */ public function getPassword() { return $this->password; } /** * Set name * * @param string $name * @return Users */ public function setName($name) { $this->name= $name; return $this; } /** * Get firstname * * @return string */ public function getFirstname() { return $this->firstname; } ?> |
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.