PHPackages                             tourze/doctrine-helper - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Database &amp; ORM](/categories/database)
4. /
5. tourze/doctrine-helper

ActiveLibrary[Database &amp; ORM](/categories/database)

tourze/doctrine-helper
======================

Doctrine Helper

1.0.0(6mo ago)033.3k20MITPHPCI passing

Since Mar 25Pushed 4mo ago1 watchersCompare

[ Source](https://github.com/tourze/doctrine-helper)[ Packagist](https://packagist.org/packages/tourze/doctrine-helper)[ RSS](/packages/tourze-doctrine-helper/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (4)Dependencies (5)Versions (5)Used By (20)

Doctrine Helper
===============

[](#doctrine-helper)

[English](README.md) | [中文](README.zh-CN.md)

[![Latest Version](https://camo.githubusercontent.com/641fbf9a91591ce39de1bf12e406191244e6c0d66db74c76c83461f745706d22/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f746f75727a652f646f637472696e652d68656c7065722e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/tourze/doctrine-helper)[![License](https://camo.githubusercontent.com/942e017bf0672002dd32a857c95d66f28c5900ab541838c6c664442516309c8a/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d626c75652e7376673f7374796c653d666c61742d737175617265)](LICENSE)[![PHP Version Require](https://camo.githubusercontent.com/0692477ea9335f003a85cc496bba5684f4c1e74a92d38eebcf3f2f89823fc7d6/687474703a2f2f706f7365722e707567782e6f72672f746f75727a652f646f637472696e652d68656c7065722f726571756972652f706870)](https://packagist.org/packages/tourze/doctrine-helper)[![Build Status](https://camo.githubusercontent.com/07a93c6c3a828e1e929e2a173231acc84705e6f7a486f6d8d0f4103f204a79d1/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6275696c642d70617373696e672d627269676874677265656e2e7376673f7374796c653d666c61742d737175617265)](https://github.com/tourze/php-monorepo)[![Coverage Status](https://camo.githubusercontent.com/994d68acb2aadf1c3c7711ddcb3b30429246a6c05da9e601f62c3f932ac795a8/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f636f7665726167652d3130302532352d627269676874677265656e2e7376673f7374796c653d666c61742d737175617265)](https://github.com/tourze/php-monorepo)[![Total Downloads](https://camo.githubusercontent.com/6f3e5d3d53866bfac371153c11ae5057ac26608f26886847787095801aba9a60/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f746f75727a652f646f637472696e652d68656c7065722e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/tourze/doctrine-helper)

A lightweight helper library to simplify and optimize your usage of Doctrine ORM in PHP projects.

Table of Contents
-----------------

[](#table-of-contents)

- [Features](#features)
- [Requirements](#requirements)
- [Installation](#installation)
- [Quick Start](#quick-start)
- [Advanced Usage](#advanced-usage)
- [API Reference](#api-reference)
- [Contributing](#contributing)
- [License](#license)

Features
--------

[](#features)

- **CacheHelper**: Generate cache IDs and tags for Doctrine entities and objects with table name support
- **EntityDetector**: Determine whether a class is a Doctrine entity using attributes
- **ReflectionHelper**: Simplify reflection operations for Doctrine entities, properties, and custom attributes, with built-in caching for performance
- **SortableTrait**: Add sorting functionality to Doctrine entities with a simple trait

Requirements
------------

[](#requirements)

- PHP &gt;= 8.1
- doctrine/orm &gt;= 3.0
- doctrine/common &gt;= 3.5
- doctrine/dbal &gt;= 4.0

Installation
------------

[](#installation)

```
composer require tourze/doctrine-helper
```

Quick Start
-----------

[](#quick-start)

### CacheHelper Example

[](#cachehelper-example)

```
use Tourze\DoctrineHelper\CacheHelper;

// Get cache ID for a class
$cacheId = CacheHelper::getClassId(User::class);

// Get cache tags for a class
$tags = CacheHelper::getClassTags(User::class);

// Get cache tags for a class with ID
$tagsWithId = CacheHelper::getClassTags(User::class, '1');

// Get cache tags for an object
$objectTags = CacheHelper::getObjectTags($user);
```

### EntityDetector Example

[](#entitydetector-example)

```
use Tourze\DoctrineHelper\EntityDetector;

// Check if a class is a Doctrine entity
if (EntityDetector::isEntityClass(User::class)) {
    // This is a Doctrine entity
}
```

### ReflectionHelper Example

[](#reflectionhelper-example)

```
use Tourze\DoctrineHelper\ReflectionHelper;

// Get class reflection
$reflection = ReflectionHelper::getClassReflection(User::class);

// Get property reflection
$property = ReflectionHelper::getReflectionProperty($user, 'email');

// Get all properties
$properties = ReflectionHelper::getProperties(User::class);

// Get all methods
$methods = ReflectionHelper::getMethods(User::class);

// Get property attributes
$attributes = ReflectionHelper::getPropertyAttributes($reflection, SomeAttribute::class);

// Get class attributes
$classAttributes = ReflectionHelper::getClassAttributes($reflection, SomeAttribute::class);

// Check if class has specific attributes
$hasAttribute = ReflectionHelper::hasClassAttributes($reflection, SomeAttribute::class);
```

### SortableTrait Example

[](#sortabletrait-example)

```
use Doctrine\ORM\Mapping as ORM;
use Tourze\DoctrineHelper\SortableTrait;

#[ORM\Entity]
class Product
{
    use SortableTrait;

    // Your entity properties here
}

// Usage
$product = new Product();
$product->setSortNumber(10);
$sortNumber = $product->getSortNumber();
$sortableArray = $product->retrieveSortableArray();
```

Advanced Usage
--------------

[](#advanced-usage)

### Performance Features

[](#performance-features)

- **Reflection caching**: All reflection operations are cached internally for better performance
- **Attribute reading**: Full support for PHP 8+ attributes
- **Memory efficient**: Optimized for minimal memory usage

### Working with Attributes

[](#working-with-attributes)

The ReflectionHelper provides powerful methods for working with PHP 8+ attributes:

```
use Tourze\DoctrineHelper\ReflectionHelper;

$reflection = ReflectionHelper::getClassReflection(MyEntity::class);

// Get all instances of a specific attribute from properties
$attributes = ReflectionHelper::getPropertyAttributes($reflection, MyAttribute::class);

// Get first instance of a specific attribute from properties
$attribute = ReflectionHelper::getPropertyAttribute($reflection, MyAttribute::class);

// Iterate through class attributes
foreach (ReflectionHelper::getClassAttributes($reflection, MyAttribute::class) as $attr) {
    // Process each attribute instance
}

// Check if class has specific attributes
if (ReflectionHelper::hasClassAttributes($reflection, MyAttribute::class)) {
    // Handle accordingly
}
```

### Cache Management

[](#cache-management)

CacheHelper provides flexible caching strategies:

```
use Tourze\DoctrineHelper\CacheHelper;

// Generate stable cache keys
$key = CacheHelper::getClassId(User::class);  // Returns: 'User'

// Generate cache tags for invalidation
$tags = CacheHelper::getClassTags(User::class);       // Returns: ['User']
$tags = CacheHelper::getClassTags(User::class, '123'); // Returns: ['User', 'User:123']

// Get tags from entity instances
$user = new User();
$user->setId(456);
$tags = CacheHelper::getObjectTags($user);  // Returns: ['User', 'User:456']
```

API Reference
-------------

[](#api-reference)

### CacheHelper

[](#cachehelper)

- `getClassId(string $className): string`
- `getClassTags(string $className, string $id = null): array`
- `getObjectTags(object $object): array`

### EntityDetector

[](#entitydetector)

- `isEntityClass(string $className): bool`

### ReflectionHelper

[](#reflectionhelper)

- `getClassReflection(object|string $object): \ReflectionClass`
- `getReflectionProperty(object $object, string $propertyName): ?\ReflectionProperty`
- `getProperties(object|string $object, $filter = null): array`
- `getMethods(object|string $object, $filter = null): array`
- `getParentClasses(\ReflectionClass $reflectionClass): array`
- `getPropertyAttributes(\ReflectionClass $reflectionClass, string $attributeName): array`
- `getPropertyAttribute(\ReflectionClass $reflectionClass, string $attributeName): ?object`
- `getClassAttributes(\ReflectionClass $reflectionClass, string $attributeName): \Traversable`
- `hasClassAttributes(\ReflectionClass $reflectionClass, string $attributeName): bool`

### SortableTrait

[](#sortabletrait)

- `setSortNumber(int $sortNumber): self`
- `getSortNumber(): int`
- `retrieveSortableArray(): array`

Contributing
------------

[](#contributing)

- Issues and pull requests are welcome.
- Follow PSR-12 coding style.
- Ensure all PHPUnit tests pass before submitting.

License
-------

[](#license)

MIT License. See [LICENSE](LICENSE) for details.

Changelog
---------

[](#changelog)

See Git commit history for changes and releases.

###  Health Score

39

—

LowBetter than 86% of packages

Maintenance71

Regular maintenance activity

Popularity21

Limited adoption so far

Community18

Small or concentrated contributor base

Maturity40

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~73 days

Total

4

Last Release

192d ago

Major Versions

0.0.3 → 1.0.02025-11-01

### Community

Maintainers

![](https://www.gravatar.com/avatar/e354fdb316da535dfa8ba2e9193a473c403b6bc6fb9170778d1dc50e304c6e9d?d=identicon)[tourze](/maintainers/tourze)

---

Top Contributors

[![tourze](https://avatars.githubusercontent.com/u/13899502?v=4)](https://github.com/tourze "tourze (1 commits)")

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Type Coverage Yes

### Embed Badge

![Health badge](/badges/tourze-doctrine-helper/health.svg)

```
[![Health](https://phpackages.com/badges/tourze-doctrine-helper/health.svg)](https://phpackages.com/packages/tourze-doctrine-helper)
```

###  Alternatives

[scienta/doctrine-json-functions

A set of extensions to Doctrine that add support for json query functions.

58523.9M36](/packages/scienta-doctrine-json-functions)[laravel-doctrine/orm

An integration library for Laravel and Doctrine ORM

8425.3M87](/packages/laravel-doctrine-orm)[damienharper/auditor-bundle

Integrate auditor library in your Symfony projects.

4542.8M](/packages/damienharper-auditor-bundle)[sonata-project/entity-audit-bundle

Audit for Doctrine Entities

644989.8k1](/packages/sonata-project-entity-audit-bundle)[bartlett/php-compatinfo-db

Reference Database of all functions, constants, classes, interfaces on PHP standard distribution and about 110 extensions

1183.0k1](/packages/bartlett-php-compatinfo-db)[heymoon/doctrine-psql-enum

Store PHP native enums as PostgeSQL custom enum types

254.9k](/packages/heymoon-doctrine-psql-enum)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
