SortableBehavior
extends Behavior
in package
Sortable Behavior
Position-based list ordering with group support. Provides toTop, toBottom, move, moveBefore, and moveAfter operations with automatic conflict resolution.
Tags
Table of Contents
Properties
- $_defaultConfig : array<string|int, mixed>
- Default configuration.
- $fields : array<string|int, mixed>
- $row : EntityInterface
- $preventCallOfMoveInEventListener : bool
Methods
- beforeSave() : void
- Auto-assign position on save for new entities or handle position changes.
- getLast() : float|int
- Returns the highest value
- getNew() : float|int
- Get position for new entity (end of list).
- getStart() : float|int
- Get configured starting position.
- getStep() : float|int
- Get configured step increment.
- initialize() : void
- Initialize behavior.
- isFirst() : bool
- Checks if it is the first row of its group
- move() : bool
- Move entity to specific position.
- moveAfter() : bool
- Move entity to position after another entity.
- moveBefore() : bool
- Move entity to position before another entity.
- toBottom() : bool
- Move entity to last position in its group.
- toTop() : bool
- Move entity to first position in its group.
- _change() : void
- Subtract or add a step to the value of a field.
- _getConditions() : array<string|int, mixed>
- Returns conditions for the WHERE clause
- _insert() : void
- Moves all values to insert a new one in the middle of the list
Properties
$_defaultConfig
Default configuration.
protected
array<string|int, mixed>
$_defaultConfig
= ['field' => 'position', 'group' => [], 'start' => 1, 'step' => 1]
$fields
protected
array<string|int, mixed>
$fields
Fields for position queries
$row
protected
EntityInterface
$row
Current entity being processed
$preventCallOfMoveInEventListener
private
bool
$preventCallOfMoveInEventListener
= false
Prevents recursion during beforeSave
Methods
beforeSave()
Auto-assign position on save for new entities or handle position changes.
public
beforeSave(EventInterface $event, EntityInterface $entity) : void
Parameters
- $event : EventInterface
-
The beforeSave event
- $entity : EntityInterface
-
The entity being saved
getLast()
Returns the highest value
public
getLast([array<string|int, mixed> $conditions = [] ]) : float|int
Parameters
- $conditions : array<string|int, mixed> = []
Return values
float|intgetNew()
Get position for new entity (end of list).
public
getNew([array<string|int, mixed> $conditions = [] ]) : float|int
Parameters
- $conditions : array<string|int, mixed> = []
-
Optional group conditions
Return values
float|int —Position for new entity
getStart()
Get configured starting position.
public
getStart() : float|int
Return values
float|int —Starting position value
getStep()
Get configured step increment.
public
getStep() : float|int
Return values
float|int —Step value
initialize()
Initialize behavior.
public
initialize(array<string|int, mixed> $config) : void
Parameters
- $config : array<string|int, mixed>
-
Configuration
isFirst()
Checks if it is the first row of its group
public
isFirst([array<string|int, mixed> $conditions = [] ]) : bool
Parameters
- $conditions : array<string|int, mixed> = []
Return values
boolmove()
Move entity to specific position.
public
move(int $id, int $newVal[, bool $moveOwn = true ]) : bool
Parameters
- $id : int
-
Primary key of entity
- $newVal : int
-
Target position
- $moveOwn : bool = true
-
Whether to move the entity itself (default: true)
Return values
bool —True on success
moveAfter()
Move entity to position after another entity.
public
moveAfter(int $id, int $afterId) : bool
Parameters
- $id : int
-
Primary key of entity to move
- $afterId : int
-
Primary key of entity to move after
Return values
bool —True on success
moveBefore()
Move entity to position before another entity.
public
moveBefore(int $id, int $beforeId) : bool
Parameters
- $id : int
-
Primary key of entity to move
- $beforeId : int
-
Primary key of entity to move before
Return values
bool —True on success
toBottom()
Move entity to last position in its group.
public
toBottom(int $id) : bool
Parameters
- $id : int
-
Primary key of entity
Return values
bool —True on success
toTop()
Move entity to first position in its group.
public
toTop(int $id) : bool
Parameters
- $id : int
-
Primary key of entity
Return values
bool —True on success
_change()
Subtract or add a step to the value of a field.
private
_change(array<string|int, mixed>|int $value[, bool $substract = true ]) : void
Parameters
- $value : array<string|int, mixed>|int
-
the new value or an array with two values
- $substract : bool = true
-
by default subtracts
_getConditions()
Returns conditions for the WHERE clause
private
_getConditions() : array<string|int, mixed>
Return values
array<string|int, mixed>_insert()
Moves all values to insert a new one in the middle of the list
private
_insert(array<string|int, mixed>|int $value) : void
Parameters
- $value : array<string|int, mixed>|int
-
the position where it will be inserted