tourze/doctrine-indexed-bundle
最新稳定版本:1.0.4
Composer 安装命令:
composer require tourze/doctrine-indexed-bundle
包简介
A Symfony bundle that enhances Doctrine ORM with automatic index management using PHP 8 attributes
README 文档
README
A Symfony bundle that enhances Doctrine ORM with automatic index management.
Features
- Automatically adds indexes to specified entity properties
- Supports regular, fulltext, and unique indexes
- Smart index naming strategy, automatically handles long table names
- Attribute-based configuration (PHP 8 attributes), no extra config files
- Zero configuration required, works out-of-the-box
Requirements
- PHP 8.1 or higher
- Symfony 6.4 or higher
- Doctrine Bundle 2.13 or higher
Installation
composer require tourze/doctrine-indexed-bundle
Usage
- Register the bundle in
config/bundles.php:
return [ // ... Tourze\DoctrineIndexedBundle\DoctrineIndexedBundle::class => ['all' => true], ];
- Use attributes to mark entity fields that require indexes:
use Tourze\DoctrineIndexedBundle\Attribute\IndexColumn; use Tourze\DoctrineIndexedBundle\Attribute\FulltextColumn; use Tourze\DoctrineIndexedBundle\Attribute\UniqueColumn; class YourEntity { #[ORM\Column] #[IndexColumn] // Regular index private string $name; #[ORM\Column(type: 'text')] #[FulltextColumn] // Fulltext index private string $description; #[ORM\Column] #[UniqueColumn] // Unique index private string $email; }
Configuration
This bundle requires no configuration and works out-of-the-box. However,
you can customize index names by providing a name parameter to the attributes:
#[IndexColumn(name: 'custom_index_name')] private string $field;
Index Naming Convention
The bundle automatically generates index names as follows:
- Regular index:
{table_name}_idx_{column_name} - Fulltext index:
{table_name}_fulltext_{column_name} - Unique constraint:
{table_name}_unique_{column_name}
If the generated name exceeds the maximum length (64 characters), the bundle will use MD5 hashing to create a shorter name while maintaining uniqueness.
Advanced Usage
Custom Index Names
You can specify custom names for indexes:
#[IndexColumn(name: 'search_idx')] private string $searchable; #[FulltextColumn(name: 'content_fulltext')] private string $content; #[UniqueColumn(name: 'email_unique')] private string $email;
Multiple Attributes
You can combine different index types on the same entity:
class Product { #[ORM\Column] #[IndexColumn] // For searching #[UniqueColumn] // For uniqueness private string $sku; #[ORM\Column(type: 'text')] #[FulltextColumn] // For full-text search #[IndexColumn] // For regular search too private string $description; }
How It Works
- Listens to Doctrine's
loadClassMetadataevent and scans entity properties - Checks for
IndexColumn,FulltextColumn, orUniqueColumnattributes on properties - Automatically adds the corresponding index to the entity field
- Naming strategy ensures compatibility with DBMS index name length limits
Contributing
Feel free to submit issues and pull requests to help improve this bundle.
License
MIT License
统计信息
- 总下载量: 36.43k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 1
- 依赖项目数: 126
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-03-24