tourze/doctrine-user-bundle

Doctrine User Bundle

0.0.3 2025-03-30 15:04 UTC

This package is auto-updated.

Last update: 2025-03-30 15:05:15 UTC


README

English | 中文

Latest Version Total Downloads License

A Symfony Bundle for automatically tracking entity creators and updaters.

Features

  • Automatically track entity creators and updaters
  • Simple integration using PHP 8 Attributes
  • Works with Symfony Security component
  • No database schema modifications required
  • Compatible with Symfony 6.4+ applications
  • Zero configuration required

Installation

Install this package via Composer:

composer require tourze/doctrine-user-bundle

Requirements

  • PHP 8.1+
  • Symfony 6.4+ or 7.1+
  • Doctrine ORM

Configuration

In your Symfony project, ensure the bundle is registered in the config/bundles.php file:

return [
    // ...
    Tourze\DoctrineUserBundle\DoctrineUserBundle::class => ['all' => true],
];

The bundle will be automatically configured with sensible defaults.

Usage

Basic Usage

In your entity classes, use the CreateUserColumn and UpdateUserColumn attributes to mark user fields:

<?php

namespace App\Entity;

use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Security\Core\User\UserInterface;
use Tourze\DoctrineUserBundle\Attribute\CreateUserColumn;
use Tourze\DoctrineUserBundle\Attribute\UpdateUserColumn;

#[ORM\Entity]
class YourEntity
{
    // ...

    #[ORM\ManyToOne(targetEntity: User::class)]
    #[CreateUserColumn]
    private ?UserInterface $createdBy = null;

    #[ORM\ManyToOne(targetEntity: User::class)]
    #[UpdateUserColumn]
    private ?UserInterface $updatedBy = null;

    // ...

    public function getCreatedBy(): ?UserInterface
    {
        return $this->createdBy;
    }

    public function getUpdatedBy(): ?UserInterface
    {
        return $this->updatedBy;
    }
}

When an entity is created or updated, the marked fields will automatically be set to the currently logged-in user.

Alternative Attributes

The bundle also provides additional attributes for more specific use cases:

  • CreatedByColumn: Records only the essential information about the creator
  • UpdatedByColumn: Records only the essential information about the updater

How It Works

This bundle leverages Doctrine's event system to automatically set the user fields when entities are persisted or updated. The UserListener class subscribes to Doctrine's prePersist and preUpdate events and sets the appropriate user fields based on the current security context.

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

License

This project is licensed under the MIT License. See the LICENSE file for details.