bengor-user/doctrine-orm-bridge
Doctrine ORM bridge for BenGorUser: adapters and persistence integration to make the User model compatible with Doctrine ORM. Install via Composer and run the fully tested PHPSpec suite; documentation lives in the main User library docs.
Install the Package
composer require bengor-user/doctrine-orm-bridge
Ensure your project meets the requirement (PHP >= 5.5).
Configure Doctrine ORM
Add the bridge to your Doctrine configuration (config/packages/doctrine.yaml):
doctrine:
orm:
entity_managers:
default:
mappings:
BenGorUser:
type: attribute
dir: "%kernel.project_dir%/vendor/bengor-user/user/src/Entity"
prefix: "BenGorUser\Entity"
alias: BenGorUser
First Use Case: User Entity Integration
Extend the BenGorUser\Entity\User class in your own entity:
namespace App\Entity;
use BenGorUser\Entity\User as BaseUser;
class User extends BaseUser
{
// Add custom fields or overrides here
}
Register the entity in your Doctrine configuration (as shown above).
User with Doctrine ORMExtend the Base User Entity
Create a custom user entity extending BenGorUser\Entity\User:
namespace App\Entity;
use BenGorUser\Entity\User;
use Doctrine\ORM\Mapping as ORM;
#[ORM\Entity(repositoryClass: "App\Repository\UserRepository")]
class User extends User
{
#[ORM\Column(type: "string", length: 255, nullable: true)]
private ?string $customField = null;
// Getters/setters for custom fields
}
Repository Integration Use the default repository or extend it:
namespace App\Repository;
use BenGorUser\Entity\User;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Persistence\ManagerRegistry;
class UserRepository extends ServiceEntityRepository
{
public function __construct(ManagerRegistry $registry)
{
parent::__construct($registry, User::class);
}
// Custom query methods (e.g., findByRole)
}
Leverage Doctrine Features Use Doctrine’s lifecycle callbacks, associations, and DQL:
#[ORM\PrePersist]
public function setCreatedAt(): void
{
$this->createdAt = new \DateTime();
}
#[ORM\ManyToMany(targetEntity: "App\Entity\Role")]
private Collection $roles;
Authentication Integration Use Symfony’s security component with the Doctrine user provider:
# config/packages/security.yaml
security:
providers:
app_user_provider:
entity:
class: App\Entity\User
property: email
UserType from BenGorUser for form integration:
use BenGorUser\Form\UserType;
$builder->add('user', UserType::class);
use Symfony\Component\Validator\Constraints as Assert;
#[Assert\NotBlank]
#[Assert\Email]
private ?string $email = null;
// src/EventListener/UserListener.php
public function postPersist(User $user, LifecycleEventArgs $event)
{
$user->setLastLogin(new \DateTime());
}
Outdated Package
Namespace Conflicts
BenGorUser namespace. Avoid naming collisions in your project:
// Bad: Overlapping namespace
namespace BenGorUser\Entity; // Conflict!
App\Entity\User extending BenGorUser\Entity\User.Missing Documentation
No Built-in Migrations
php bin/console make:migration
Entity Not Found Errors
mappings configuration in doctrine.yaml points to the correct directory:
dir: "%kernel.project_dir%/vendor/bengor-user/user/src/Entity"
prefix and alias.Property Not Mapped
#[ORM\Column(type: "string")]
private $customField;
php bin/console doctrine:schema:validate to check mappings.Authentication Issues
UserInterface is implemented (check BenGorUser\Entity\User).property (e.g., email) matches the user entity’s field.Custom User Fields Extend the base entity and add fields:
#[ORM\Column(type: "json")]
private array $metadata = [];
Override Methods
Override methods like getRoles() or getPassword():
public function getRoles(): array
{
$roles = $this->roles->toArray();
return empty($roles) ? ['ROLE_USER'] : $roles;
}
Add Soft Deletes
Use a library like stof/doctrine-extensions for soft deletes:
#[ORM\Column(type: "boolean")]
#[SoftDeleteable]
private $deleted = false;
Event Subscribers
Listen to user events (e.g., preUpdate):
#[AsEventListener(event: "preUpdate", method: "onPreUpdate")]
public function onPreUpdate(User $user, LifecycleEventArgs $event)
{
$user->setUpdatedAt(new \DateTime());
}
fetch="EAGER" for frequently accessed associations:
#[ORM\ManyToMany(targetEntity: "App\Entity\Role", fetch: "EAGER")]
private Collection $roles;
email):
#[ORM\Column(type: "string", unique: true)]
#[ORM\Index(columns: ["email"])]
private ?string $email;
How can I help you explore Laravel packages today?