Defining Entities and Relationships in Doctrine ORM.
Course Title: Mastering Symfony: Building Enterprise-Level PHP Applications Section Title: Doctrine ORM and Database Integration Topic: Defining entities, relationships (one-to-one, one-to-many, many-to-many)
Defining Entities
In the Doctrine ORM context, an entity represents a database table, and it's a PHP class that maps to that table. To define an entity, you need to create a PHP class that will hold the data, and then use Doctrine's annotations to configure the mapping between the class and the database table.
Let's take a look at an example of a simple entity, a User
class:
// src/Entity/User.php
namespace App\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity
* @ORM\Table(name="users")
*/
class User
{
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="string", length=255)
*/
private $username;
/**
* @ORM\Column(type="string", length=255)
*/
private $email;
// getters and setters
}
In this example, we're using Doctrine's annotations to define the mapping between the User
class and the users
database table. The @ORM\Entity
annotation indicates that this class is an entity, and the @ORM\Table
annotation specifies the table name.
Defining Relationships
In a database, relationships between tables are defined using foreign keys. In Doctrine, you can define relationships between entities using annotations. There are three types of relationships: one-to-one, one-to-many, and many-to-many.
One-to-One Relationships
A one-to-one relationship exists when one entity is associated with only one other entity. For example, a User
entity might have a one-to-one relationship with a UserProfile
entity.
// src/Entity/User.php
namespace App\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity
* @ORM\Table(name="users")
*/
class User
{
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\OneToOne(targetEntity="UserProfile")
* @ORM\JoinColumn(name="user_profile_id", referencedColumnName="id")
*/
private $userProfile;
// getters and setters
}
// src/Entity/UserProfile.php
namespace App\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity
* @ORM\Table(name="user_profiles")
*/
class UserProfile
{
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
*/
private $id;
// getters and setters
}
In this example, we're using the @ORM\OneToOne
annotation to define a one-to-one relationship between the User
and UserProfile
entities.
One-to-Many Relationships
A one-to-many relationship exists when one entity is associated with multiple other entities. For example, a Category
entity might have a one-to-many relationship with multiple Product
entities.
// src/Entity/Category.php
namespace App\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity
* @ORM\Table(name="categories")
*/
class Category
{
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\OneToMany(targetEntity="Product", mappedBy="category")
*/
private $products;
// getters and setters
}
// src/Entity/Product.php
namespace App\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity
* @ORM\Table(name="products")
*/
class Product
{
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\ManyToOne(targetEntity="Category", inversedBy="products")
* @ORM\JoinColumn(name="category_id", referencedColumnName="id")
*/
private $category;
// getters and setters
}
In this example, we're using the @ORM\OneToMany
annotation to define a one-to-many relationship between the Category
and Product
entities.
Many-to-Many Relationships
A many-to-many relationship exists when multiple entities are associated with multiple other entities. For example, a Product
entity might have a many-to-many relationship with multiple Tag
entities.
// src/Entity/Product.php
namespace App\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity
* @ORM\Table(name="products")
*/
class Product
{
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\ManyToMany(targetEntity="Tag", inversedBy="products")
* @ORM\JoinTable(name="product_tags",
* joinColumns={@ORM\JoinColumn(name="product_id", referencedColumnName="id")},
* inverseJoinColumns={@ORM\JoinColumn(name="tag_id", referencedColumnName="id")}
* )
*/
private $tags;
// getters and setters
}
// src/Entity/Tag.php
namespace App\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity
* @ORM\Table(name="tags")
*/
class Tag
{
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\ManyToMany(targetEntity="Product", mappedBy="tags")
*/
private $products;
// getters and setters
}
In this example, we're using the @ORM\ManyToMany
annotation to define a many-to-many relationship between the Product
and Tag
entities.
Conclusion
In this topic, we covered how to define entities and relationships in Doctrine ORM. We saw how to use annotations to configure the mapping between entities and database tables, and how to define one-to-one, one-to-many, and many-to-many relationships between entities.
Practical Takeaways
- Use Doctrine's annotations to define the mapping between entities and database tables.
- Use the
@ORM\Entity
annotation to define an entity. - Use the
@ORM\Table
annotation to specify the table name. - Use the
@ORM\OneToOne
,@ORM\OneToMany
, and@ORM\ManyToMany
annotations to define relationships between entities. - Use the
mappedBy
attribute to specify the inverse side of a relationship.
Further Reading
Leave a comment or ask for help if you have any questions or need further clarification on any of the concepts covered in this topic.
In the next topic, we will cover how to use Doctrine's QueryBuilder and repository pattern to perform database queries.
Images

Comments