1. Go to this page and download the library: Download julienlinard/doctrine-php library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
julienlinard / doctrine-php example snippets
ulienLinard\Doctrine\EntityManager;
use JulienLinard\Doctrine\Mapping\Entity;
use JulienLinard\Doctrine\Mapping\Column;
use JulienLinard\Doctrine\Mapping\Id;
// Define an entity
#[Entity(table: 'users')]
class User
{
#[Id]
#[Column(type: 'integer', autoIncrement: true)]
public ?int $id = null;
#[Column(type: 'string', length: 255)]
public string $email;
#[Column(type: 'string', length: 255)]
public string $name;
}
// Database configuration
$config = [
'driver' => 'mysql',
'host' => 'localhost',
'dbname' => 'mydatabase',
'user' => 'root',
'password' => 'password'
];
// Create Entity Manager
$em = new EntityManager($config);
// Create a user
$user = new User();
$user->email = '[email protected]';
$user->name = 'John Doe';
$em->persist($user);
$em->flush();
// Retrieve a user
$user = $em->getRepository(User::class)->find(1);
echo $user->name; // John Doe
use JulienLinard\Doctrine\Mapping\Entity;
use JulienLinard\Doctrine\Mapping\Column;
use JulienLinard\Doctrine\Mapping\Id;
use JulienLinard\Doctrine\Mapping\Index;
#[Entity(table: 'users')]
class User
{
#[Id]
#[Column(type: 'integer', autoIncrement: true)]
public ?int $id = null;
#[Column(type: 'string', length: 255)]
#[Index(unique: true)]
public string $email;
#[Column(type: 'string', length: 255, nullable: true)]
public ?string $name = null;
#[Column(type: 'boolean', default: true)]
public bool $is_active = true;
#[Column(type: 'datetime', nullable: true)]
public ?\DateTime $created_at = null;
}
// Load users with their posts (optimized - avoids N+1 queries)
$users = $repository->findAllWith(['posts']);
// Each user now has $user->posts loaded
foreach ($users as $user) {
foreach ($user->posts as $post) {
echo $post->title;
}
}
use JulienLinard\Doctrine\Repository\EntityRepository;
class UserRepository extends EntityRepository
{
public function findActiveUsers(): array
{
return $this->findBy(['is_active' => true]);
}
public function findByEmailDomain(string $domain): array
{
return $this->findBy([], ['email' => 'ASC'])
->filter(fn($user) => str_ends_with($user->email, $domain));
}
}
// Create custom repository
$userRepo = $em->createRepository(UserRepository::class, User::class);
$activeUsers = $userRepo->findActiveUsers();
use JulienLinard\Doctrine\Mapping\OneToMany;
use JulienLinard\Doctrine\Mapping\ManyToOne;
#[Entity(table: 'users')]
class User
{
#[Id]
#[Column(type: 'integer', autoIncrement: true)]
public ?int $id = null;
#[OneToMany(targetEntity: Post::class, mappedBy: 'user', cascade: ['persist', 'remove'])]
public array $posts = [];
}
#[Entity(table: 'posts')]
class Post
{
#[Id]
#[Column(type: 'integer', autoIncrement: true)]
public ?int $id = null;
#[ManyToOne(targetEntity: User::class, inversedBy: 'posts')]
public ?User $user = null;
#[Column(type: 'string', length: 255)]
public string $title;
}
// Usage
$user = $em->getRepository(User::class)->find(1);
// Load relations manually
$em->loadRelations($user, 'posts');
// Or use eager loading (optimized)
$users = $repository->findAllWith(['posts']);
use JulienLinard\Doctrine\Mapping\ManyToMany;
#[Entity(table: 'users')]
class User
{
#[ManyToMany(targetEntity: Role::class)]
public array $roles = [];
}
#[Entity(table: 'roles')]
class Role
{
#[Id]
#[Column(type: 'integer', autoIncrement: true)]
public ?int $id = null;
#[Column(type: 'string', length: 50)]
public string $name;
}
$user = $em->transaction(function($em) {
$user = new User();
$user->email = '[email protected]';
$em->persist($user);
$em->flush();
return $user;
});
// Automatically commits on success, rolls back on exception