SortableRelation adds sorting support to pivot relationships


In the model class definition add:

use \October\Rain\Database\Traits\SortableRelation;

public $belongsToMany = [..., 'pivotSortable' => 'sort_order_column'];

To set orders:

$model->setSortableRelationOrder($relationName, $recordIds, $recordOrders);

Protected Properties

protected array $sortableRelationDefinitions

Public Methods

public getRelationSortOrderColumn()

public getRelationSortOrderColumn($relation): string 

getRelationSortOrderColumn gets the name of the "sort_order" column.

public initializeSortableRelation()

public initializeSortableRelation(): void

initializeSortableRelation trait for the model.

public isSortableRelation()

public isSortableRelation($relationName): void

isSortableRelation returns true if the supplied relation is sortable.

public setSortableRelationOrder()

public setSortableRelationOrder(
    string $relationName,
    mixed $itemIds,
    array|null|bool $referencePool = null
): void

setSortableRelationOrder sets the sort order of records to the specified orders. If the orders is undefined, the record identifier is used. If reference pool is true, then an incrementing pool is used.

Protected Methods

protected defineSortableRelations()

protected defineSortableRelations(): void

defineSortableRelations will spin over every relation and check for pivotSortable mode

protected getSortableRelations()

protected getSortableRelations(): array 

getSortableRelations returns all configured sortable relations.

protected processSortableRelationOrdersInternal()

protected processSortableRelationOrdersInternal(
    $referencePool = null
): array