Entities

Contents
An entity class
Map columns
Ids that generate
Repository class

An entity class

An entity is a PHP class mapped to one database table. Doctrine reads the attributes and builds the schema from them. Entities live in src/Entity:

namespace App\Entity; use Doctrine\ORM\Mapping as ORM; #[ORM\Entity] class Post { #[ORM\Id] #[ORM\GeneratedValue] #[ORM\Column] private ?int $id = null; }

Map columns

Every property becomes a table column. Options like length and type tune the column without touching SQL:

use Doctrine\ORM\Mapping as ORM; class Post { #[ORM\Column(length: 255)] private string $title = ""; #[ORM\Column(type: "text")] private string $body = ""; }

Ids that generate

The id property is special. GeneratedValue tells the database to number new rows itself, so your code never assigns it. A getter exposes it for reading:

class Post { public function getId(): ?int { return $this->id; } public function getTitle(): string { return $this->title; } }

Repository class

Each entity gets a repository class for queries. It extends the base repository and receives the registry through its constructor, exactly as the entity generator writes it:

namespace App\Repository; use App\Entity\Post; use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository; use Doctrine\Persistence\ManagerRegistry; class PostRepository extends ServiceEntityRepository { public function __construct(ManagerRegistry $registry) { parent::__construct($registry, Post::class); } }

Next: Migrations

Article author: Arthur Isaev

Related articles
Development with PHP
What is Symfony and where it runs
Install Symfony
Your first page
Routes
Controllers
Twig basics
Twig advanced
Doctrine setup
Entities
Migrations
Repositories
Relations
Forms
Validation
Services and DI
JSON controllers
Serializer
Security intro
Users
Login form
Voters
Console commands
Events
Messenger
Mailer
Cache
Sessions
CSRF
Translations
Dotenv and env
Profiler
Testing
Fixtures
Pagination
File uploads
API Platform in one lesson
DQL
Transactions
Migrations advanced
Security advanced
HTTP client
Scheduler
Webhooks
Asset Mapper
Deployment
Performance
Capstone: blog
Capstone: JSON API
Modern Symfony tour
Hero roadmap: the whole course on one page
Banner image

Search this site

Channel @aofeed Chat @aofeedchat

Contacts and cooperation:
I recommend our hosting beget.com
Write to info@aredel.com if you:
1. Want to write an article for our site or translate an article into your native language.
2. Want to place thematically relevant ads on the site.
3. Ads on my site pass maximum censorship. If you see an ad block unsuitable for school-age children, shocking or misleading - please contact us by e-mail
4. Found a mistake, inaccuracy, bug, etc. on the site. ... .......
5. Articles can be shared on social media by clicking a network icon: