Download the PHP package chamber-orchestra/doctrine-sort-bundle without Composer
On this page you can find all versions of the php package chamber-orchestra/doctrine-sort-bundle. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download chamber-orchestra/doctrine-sort-bundle
More information about chamber-orchestra/doctrine-sort-bundle
Files in chamber-orchestra/doctrine-sort-bundle
Package doctrine-sort-bundle
Short Description Symfony bundle for automatic sort order management in Doctrine ORM entities. Recalculates positions on flush with grouped ordering, second-level cache support, and PHP attribute configuration.
License MIT
Homepage https://github.com/chamber-orchestra/doctrine-sort-bundle
Informations about the package doctrine-sort-bundle
Doctrine Sort Bundle
Symfony bundle for automatic sort order management in Doctrine ORM entities. Recalculates sort positions on every EntityManager::flush(), keeping ordered lists consistent without manual reindexing. Uses PHP attributes to mark the sort field, supports grouped ordering (e.g., per parent or category), and handles insertions, deletions, and reordering transparently.
Features
- Automatic reordering — sort positions are recalculated on flush, no manual gaps or renumbering needed
- Grouped sorting — maintain independent sort orders per parent, category, or any relation via
groupBy - PHP attribute configuration — single
#[Sort]attribute on your entity property, zero YAML/XML config - Drag-and-drop ready — set the new position and flush; surrounding items shift automatically
- Second-level cache support — works with Doctrine SLC; optionally evict cache collections and query regions on sort changes
- Change tracking policies — compatible with both
DEFERRED_IMPLICITandDEFERRED_EXPLICITtracking - Provided traits —
SortTraitandSortByParentTraitwith convenience methods (moveUp,moveDown,moveToBeginning,moveToEnd)
Installation
If Symfony Flex is enabled, the bundle is registered automatically. Otherwise, add it to config/bundles.php:
Quick Start
1. Mark a sort field
Add the #[Sort] attribute to any integer column. The bundle will automatically maintain sequential ordering starting from 1.
2. Grouped ordering
Maintain separate sort sequences per parent, category, or any relation:
3. Using traits
For common cases, use the provided traits instead of writing boilerplate:
SortTrait provides getSortOrder(), setSortOrder(), and convenience methods:
| Method | Effect |
|---|---|
moveUp() |
Decrease sort position by 1 (minimum 1) |
moveDown() |
Increase sort position by 1 |
moveToBeginning() |
Set sort position to 1 |
moveToEnd() |
Set sort position to 0 (appends to end) |
SortByParentTrait extends SortTrait with a $parent ManyToOne field and #[Sort(groupBy: ['parent'])].
Attribute Options
| Option | Type | Default | Description |
|---|---|---|---|
groupBy |
array |
[] |
Entity field names that define sort groups. Each unique combination of group values has its own sort sequence. Supports Column, ManyToOne, and ManyToMany fields. |
evictCollections |
array |
[] |
List of [FQCN, collectionField] pairs. These Doctrine second-level cache collections are evicted when sort order changes. |
evictRegions |
array |
[] |
Query cache region names to evict when sort order changes. |
How It Works
- The bundle listens to Doctrine's
onFlushevent - For each entity with a
#[Sort]attribute that was inserted, updated, or deleted, it collects the changes - Overlapping changes within the same group are merged into ranges to minimize database queries
- Affected entities are fetched, reordered in memory, and sort values are reassigned sequentially
- Doctrine's
recomputeSingleEntityChangeSet()ensures the corrected values are persisted in the same flush
This means you never need to manually manage gaps, shift items, or renumber sequences — just set the desired position and flush. Multiple reorder operations within a single flush or across multiple flushes in an explicit transaction are fully supported.
Compatibility
| Bundle | PHP | Symfony | Doctrine ORM |
|---|---|---|---|
| 8.0 | ^8.5 | 8.0 | 3.6+ |
Running Tests
License
MIT