eelan/doctrine-traits

A collection of traits for Doctrine entities.

0.0.2 2025-04-13 17:17 UTC

This package is auto-updated.

Last update: 2025-05-13 17:34:36 UTC


README

Reusable PHP traits for Doctrine entities, designed for Symfony projects.

This library provides a collection of common traits to reduce boilerplate code when working with Doctrine ORM.

๐Ÿ“ฆ Installation

Install via Composer:

composer require eelan/doctrine-traits

โš ๏ธ If the package is not yet published on Packagist, you can include the GitHub repository manually in your composer.json.

๐Ÿงฑ Available Traits

Eelan\DoctrineTraits\Common

  • IdTrait โ€“ Auto-increment integer ID
  • TimestampTrait โ€“ createdAt, updatedAt (with lifecycle callbacks)
  • SoftDeleteTrait โ€“ deletedAt, with delete() and restore() methods

Eelan\DoctrineTraits\Contact

  • PhoneTrait โ€“ phone
  • EmailTrait โ€“ email

Eelan\DoctrineTraits\Personal

  • NameTrait โ€“ firstName, lastName
  • GenderTrait โ€“ gender (based on enum Gender: MALE, FEMALE, OTHER, UNKNOWN)
  • BirthTrait โ€“ birthDate, birthPlace

Eelan\DoctrineTraits\Location

  • AddressTrait โ€“ streetName, streetNumber, postalCode, city
  • ExtendedAddressTrait โ€“ extends AddressTrait and adds region, country
  • GpsCoordinateTrait โ€“ latitude, longitude, and grouped coordinate helpers

๐Ÿ›  Usage

In your Doctrine entity:

use Eelan\DoctrineTraits\Common\IdTrait;
use Eelan\DoctrineTraits\Location\AddressTrait;
use Eelan\DoctrineTraits\Contact\EmailTrait;

class Person
{
    use IdTrait;
    use AddressTrait;
    use EmailTrait;

    // your custom fields...
}

All fields are mapped using PHP 8+ attributes (#[ORM\Column(...)]).

๐Ÿงช Example Entity

See examples/Person.php for a complete example using all traits.

๐Ÿ“„ License

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

๐Ÿ™Œ Contributions

Contributions are welcome!
Feel free to open an issue or submit a pull request with new traits or improvements.