PHP code example of tourze / doctrine-helper

1. Go to this page and download the library: Download tourze/doctrine-helper 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/ */

    

tourze / doctrine-helper example snippets


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);

use Tourze\DoctrineHelper\EntityDetector;

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

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);

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();

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
}

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']