Intermediate
What is Doctrine and how is it used in Symfony?
Doctrine is an Object-Relational Mapper (ORM) that simplifies database interactions in Symfony applications. It allows developers to work with database records as PHP objects, enhancing productivity and code maintainability.
To use Doctrine in Symfony, you typically follow these steps:
- Install Doctrine: Use Composer to install Doctrine packages.
- Configure Entity Classes: Define entity classes that represent database tables.
- Database Migrations: Use migrations to create and update your database schema.
Here's a simple example of an entity class:
namespace App\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity
*/
class User {
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="string")
*/
private $name;
}