PHP Classes

How to Implement a PHP Database Table Editor to List and Move Table Records to Different Positions Using the Package Persistence: Retrieve database table records by a given order

Recommend this page to a friend!
  Info   Documentation   View files Files   Install with Composer Install with Composer   Download Download   Reputation   Support forum   Blog    
Last Updated Ratings Unique User Downloads Download Rankings
2026-07-22 (20 days ago) RSS 2.0 feedNot yet rated by the usersTotal: Not yet counted Not yet ranked
Version License PHP version Categories
persistence 1.0MIT/X Consortium ...7Databases, PHP 7
Description 

Author

This package can retrieve database table records by a given order.

It provides one class that can compose and execute SQL queries using PDO to retrieve records of database tables by an order defined using a configuration class.

The configuration class allows defining the following:

- The name of the table

- The table columns that will be used to define the sorting order and global sorting order

- The table record identifier column

- The name of the column that will be used to determine if the table record was marked as deleted

The class provides a class for:

- Retrieve the database table record in the next position

- Change the order of a given database table record

- Check if a given database record exists

The package also provides a class to retrieve database table records split in multiple pages.

Picture of Mohamed Abdulalim
  Performance   Level  
Name: Mohamed Abdulalim <contact>
Classes: 1 package by
Country: Egypt Egypt
Innovation award
Innovation award
Nominee: 1x

Instructions

Documentation

<div align="center">

Maatify Persistence

Maatify.dev

Package status:<br> Latest Version PHP Version License: MIT PHPStan: Level Max

Documentation:<br> Changelog Package Reference Security Policy Contributing Guide

Ecosystem and usage:<br> Monthly Downloads Total Downloads Maatify Ecosystem Install

Standalone, framework-agnostic PDO utilities for Maatify projects, providing robust scoped and global ordering, and pagination tools. Designed and verified for MySQL environments.

> Note: PDO Pagination is available starting with v1.1.0.

</div>

? Key Features

  • Global and Scoped Ordering: Easily manage display order across an entire table or within a specific scope.
  • Transaction Ownership: Handles its own transactions and locks the necessary scope reliably.
  • SQL Identifier Validation: Ensures table and column configurations are safe and properly quoted.
  • Soft-Delete Filtering: Optional support for ignoring soft-deleted rows in ordering calculations.
  • Scope Isolation: Ensures only the affected range within the configured scope is updated.
  • PDO Pagination: Deterministic offset pagination with strict normalization, bounds checking, and safe whitelist-based sorting.

?? Requirements

Runtime requirements: * PHP >= 8.2 * ext-pdo * maatify/exceptions ^1.0

Database behavior: * The package behavior is designed and verified against MySQL.

? Installation

composer require maatify/persistence

? Quick Usage

use Maatify\Persistence\Pdo\Ordering\ScopedOrderingConfig;
use Maatify\Persistence\Pdo\Ordering\ScopedOrderingManager;

// 1. Configure the ordering behavior for a table
$config = new ScopedOrderingConfig(
    table: 'maa_shipping_rates',
    scopeColumn: 'method_id', // Use null for global ordering
    idColumn: 'id',
    orderColumn: 'display_order',
    deletedAtColumn: 'deleted_at', // Use null if soft-deletes are not used
);

$ordering = new ScopedOrderingManager();

// 2. Get the next position for a new insert
$nextPosition = $ordering->getNextPosition(
    pdo: $pdo,
    config: $config,
    scopeValue: 2, // Use null for global ordering
);

// 3. Move an existing row within its scope
$success = $ordering->moveWithinScope(
    pdo: $pdo,
    config: $config,
    scopeValue: 2, // Use null for global ordering
    id: 15,
    newOrder: 4,
);

PDO Pagination

use Maatify\Persistence\Pdo\Pagination\PaginationConfig;
use Maatify\Persistence\Pdo\Pagination\PageRequest;
use Maatify\Persistence\Pdo\Pagination\PdoPaginationQueryDescriptor;
use Maatify\Persistence\Pdo\Pagination\PdoPaginator;
use Maatify\Persistence\Pdo\Pagination\SortWhitelist;
use Maatify\Persistence\Pdo\Pagination\SortDirectionEnum;

$config = new PaginationConfig(
    defaultPerPage: 10,
    maxPerPage: 100,
    minPerPage: 1,
    sortWhitelist: new SortWhitelist([
        'id' => 'id',
        'created' => 'created_at',
        'name' => 'user_name',
    ]),
    defaultSortBy: 'created',
    defaultSortDirection: SortDirectionEnum::DESC,
    tieBreakerSortBy: 'id',
    tieBreakerDirection: SortDirectionEnum::DESC
);

$query = new PdoPaginationQueryDescriptor(
    totalSql: 'SELECT COUNT(*) FROM users',
    totalParams: [],
    filteredCountSql: 'SELECT COUNT(*) FROM users WHERE status = :status',
    filteredCountParams: ['status' => 'active'],
    dataSql: 'SELECT id, user_name, created_at FROM users WHERE status = :status',
    dataParams: ['status' => 'active']
);

$request = new PageRequest(page: 2, perPage: 15, sortBy: 'name', sortDirection: 'ASC');

$paginator = new PdoPaginator();
$result = $paginator->paginate(
    pdo: $pdo,
    query: $query,
    request: $request,
    config: $config,
    mapper: fn(array $row) => (object) $row
);

? Public Runtime API

The package currently provides the following public classes for PDO ordering and pagination:

Maatify\Persistence\Pdo\Ordering\ScopedOrderingConfig;
Maatify\Persistence\Pdo\Ordering\ScopedOrderingManager;

Maatify\Persistence\Pdo\Pagination\PageRequest;
Maatify\Persistence\Pdo\Pagination\SortDirectionEnum;
Maatify\Persistence\Pdo\Pagination\SortWhitelist;
Maatify\Persistence\Pdo\Pagination\PaginationConfig;
Maatify\Persistence\Pdo\Pagination\PdoPaginationQueryDescriptor;
Maatify\Persistence\Pdo\Pagination\PageResult;
Maatify\Persistence\Pdo\Pagination\PdoPaginator;

// Exceptions
Maatify\Persistence\Exception\PersistenceException;
Maatify\Persistence\Exception\InvalidOrderingConfigurationException;
Maatify\Persistence\Exception\InvalidOrderingOperationException;
Maatify\Persistence\Exception\OrderingTransactionException;
Maatify\Persistence\Exception\InvalidPaginationConfigurationException;
Maatify\Persistence\Exception\InvalidPaginationQueryException;
Maatify\Persistence\Exception\PaginationExecutionException;

?? Critical Runtime Behavior

getNextPosition(): * Does not start a transaction. * Does not lock the applicable scope. * For concurrent inserts, the host application must provide the transaction and locking mechanism required to serialize position allocation.

moveWithinScope(): * Rejects inconsistent scope usage. * Rejects id <= 0. * Rejects newOrder <= 0. * Rejects caller-owned active PDO transactions. * Owns its own transaction. * Locks the applicable active scope using SELECT ... FOR UPDATE. * Reads the current order from the database within the same transaction. * Does not trust a current order provided by the caller. * Returns false if the target row is missing. * Clamps values higher than the maximum position to the maximum available position. * Returns true if the movement is a no-op (already at the requested position). * Moves only the affected range. * Does not globally normalize pre-existing gaps. * Rolls back and returns false if the final target update fails. * Rolls back on any Throwable after starting the transaction. * Rethrows the original Throwable without arbitrary wrapping.

rowExistsInScope(): * Returns false for id <= 0. * Returns false if the row is not found within the configured scope. * Treats soft-deleted rows as non-existent if deletedAtColumn is configured. * Throws InvalidOrderingOperationException on invalid scope usage. * External PDO errors propagate unmodified.

PDO Pagination: * Normalizes page and per-page limits strictly. * Uses safe whitelist-based sorting. * Host application owns SQL, scopes, mapping, and filters. * Package handles count queries and offset calculation. * Does not alter or require active PDO transactions.

?? Architecture Guarantees

  • Standalone Composer package.
  • Framework-agnostic.
  • Host-agnostic.
  • PDO-based.
  • No ORM.
  • No framework bindings.
  • No HTTP endpoints, UI, controllers, or routes.
  • No host table ownership.
  • No generic application repository abstraction.
  • The host provides the PDO connection.
  • Trusted SQL identifiers.
  • Runtime values use prepared statements.

?? Exception and Error Propagation

All package-defined exceptions implement the marker interface Maatify\Persistence\Exception\PersistenceException. However, this interface is not a catch-all. PDOException or other external Throwables may propagate without wrapping and require a separate catch or an outer Throwable boundary if handling is needed.

? Security and Trust Boundaries

The ScopedOrderingConfig validates and quotes all configured table and column identifiers. However, these identifiers must still be provided as trusted application configurations (e.g., constants), never as raw user input. All actual runtime values are safely passed using PDO prepared statements.

? Documentation

For a comprehensive guide, please refer to the main technical reference: * Persistence Package Reference

Other important documentation: * Changelog * Security Policy * Contributing Guide * Code of Conduct * Architecture Decision Records * Package Building Standard * CI Workflow Standard * Library Presentation Standard

? Quality Status

  • PHP 8.2?8.5 verification in CI.
  • PHPStan Level Max.
  • Unit, Regression, and MySQL Integration tests.
  • Lowest dependencies verification.
  • Stable CI Gate.

Integration testing: * Real MySQL is required for Integration tests. * SQLite is not an Integration substitute. * MySQL 8.4.10 is the currently verified CI baseline.

?? Development and Testing

composer validate --strict
composer analyse
composer test:unit
composer test:regression
vendor/bin/php-cs-fixer fix --dry-run --diff

composer test:integration and composer test require a real MySQL database. SQLite is explicitly not an integration substitute.

Set the following environment variables for Integration tests: * PERSISTENCE_TEST_MYSQL_DSN * PERSISTENCE_TEST_MYSQL_USER * PERSISTENCE_TEST_MYSQL_PASSWORD

? License

This project is licensed under the MIT License - see the LICENSE file for details.

? Author

Engineered by Mohamed Abdulalim (@megyptm)<br> Backend Lead & Technical Architect<br> https://www.maatify.dev

<div align="center">

Built with ?? by Maatify.dev ? Unified Ecosystem for Modern PHP Libraries

</div>


  Files folder image Files (76)  
File Role Description
Files folder image.github (1 directory)
Files folder imagedocs (3 directories)
Files folder imagesrc (2 directories)
Files folder imagetests (1 file, 5 directories)
Accessible without login Plain text file .php-cs-fixer.php Example Example script
Accessible without login Plain text file CHANGELOG.md Data Auxiliary data
Accessible without login Plain text file CODE_OF_CONDUCT.md Data Auxiliary data
Accessible without login Plain text file composer.json Data Auxiliary data
Accessible without login Plain text file CONTRIBUTING.md Data Auxiliary data
Accessible without login Plain text file LICENSE Lic. License text
Accessible without login Plain text file PERSISTENCE_PACKAGE_REFERENCE.md Data Auxiliary data
Accessible without login Plain text file phpstan.neon Data Auxiliary data
Accessible without login Plain text file phpunit.xml Data Auxiliary data
Accessible without login Plain text file README.md Doc. Documentation
Accessible without login Plain text file SECURITY.md Data Auxiliary data

  Files folder image Files (76)  /  .github  
File Role Description
Files folder imageworkflows (1 file)

  Files folder image Files (76)  /  .github  /  workflows  
File Role Description
  Accessible without login Plain text file ci.yml Example Example script

  Files folder image Files (76)  /  docs  
File Role Description
Files folder imageadr (3 files)
Files folder imagearchitecture (3 files)
Files folder imagestandards (4 files)

  Files folder image Files (76)  /  docs  /  adr  
File Role Description
  Accessible without login Plain text file 0001-pdo-pagination-architecture.md Data Auxiliary data
  Accessible without login Plain text file 0002-ordering-hard-delete-compaction.md Data Auxiliary data
  Accessible without login Plain text file README.md Doc. Documentation

  Files folder image Files (76)  /  docs  /  architecture  
File Role Description
  Accessible without login Plain text file PDO_PAGINATION_CONTRACT.md Data Auxiliary data
  Plain text file PDO_PAGINATION_RUNTIME_BLUEPRINT.md Class Class source
  Accessible without login Plain text file PDO_PAGINATION_TEST_BLUEPRINT.md Example Example script

  Files folder image Files (76)  /  docs  /  standards  
File Role Description
  Accessible without login Plain text file CI_WORKFLOW_STANDARD.md Data Auxiliary data
  Accessible without login Plain text file COMPOSER_PACKAGE_STANDARD.md Data Auxiliary data
  Accessible without login Plain text file LIBRARY_PRESENTATION_STANDARD.md Data Auxiliary data
  Accessible without login Plain text file PACKAGE_BUILDING_STANDARD.md Data Auxiliary data

  Files folder image Files (76)  /  src  
File Role Description
Files folder imageException (7 files)
Files folder imagePdo (2 directories)

  Files folder image Files (76)  /  src  /  Exception  
File Role Description
  Plain text file InvalidOrderingCon...rationException.php Class Class source
  Plain text file InvalidOrderingOperationException.php Class Class source
  Plain text file InvalidPaginationC...rationException.php Class Class source
  Plain text file InvalidPaginationQueryException.php Class Class source
  Plain text file OrderingTransactionException.php Class Class source
  Plain text file PaginationExecutionException.php Class Class source
  Plain text file PersistenceException.php Class Class source

  Files folder image Files (76)  /  src  /  Pdo  
File Role Description
Files folder imageOrdering (2 files)
Files folder imagePagination (7 files)

  Files folder image Files (76)  /  src  /  Pdo  /  Ordering  
File Role Description
  Plain text file ScopedOrderingConfig.php Class Class source
  Plain text file ScopedOrderingManager.php Class Class source

  Files folder image Files (76)  /  src  /  Pdo  /  Pagination  
File Role Description
  Plain text file PageRequest.php Class Class source
  Plain text file PageResult.php Class Class source
  Plain text file PaginationConfig.php Class Class source
  Plain text file PdoPaginationQueryDescriptor.php Class Class source
  Plain text file PdoPaginator.php Class Class source
  Accessible without login Plain text file SortDirectionEnum.php Aux. Configuration script
  Plain text file SortWhitelist.php Class Class source

  Files folder image Files (76)  /  tests  
File Role Description
Files folder imageFixtures (1 directory)
Files folder imageIntegration (1 directory)
Files folder imageRegression (2 directories)
Files folder imageSupport (2 directories)
Files folder imageUnit (1 directory)
  Accessible without login Plain text file bootstrap.php Aux. Configuration script

  Files folder image Files (76)  /  tests  /  Fixtures  
File Role Description
Files folder imageMySql (3 files)

  Files folder image Files (76)  /  tests  /  Fixtures  /  MySql  
File Role Description
  Accessible without login Plain text file create_global_ordering_table.sql Data Auxiliary data
  Accessible without login Plain text file create_pagination_items_table.sql Data Auxiliary data
  Accessible without login Plain text file create_scoped_ordering_table.sql Data Auxiliary data

  Files folder image Files (76)  /  tests  /  Integration  
File Role Description
Files folder imagePdo (2 directories)

  Files folder image Files (76)  /  tests  /  Integration  /  Pdo  
File Role Description
Files folder imageOrdering (6 files)
Files folder imagePagination (3 files)

  Files folder image Files (76)  /  tests  /  Integration  /  Pdo  /  Ordering  
File Role Description
  Plain text file ScopedOrderingManagerGlobalMoveTest.php Class Class source
  Plain text file ScopedOrderingMana...dOperationsTest.php Class Class source
  Plain text file ScopedOrderingManagerRollbackTest.php Class Class source
  Plain text file ScopedOrderingManagerScopedMoveTest.php Class Class source
  Plain text file ScopedOrderingMana...tDeleteMoveTest.php Class Class source
  Plain text file ScopedOrderingManagerTransactionTest.php Class Class source

  Files folder image Files (76)  /  tests  /  Integration  /  Pdo  /  Pagination  
File Role Description
  Plain text file PdoPaginatorFailureContractTest.php Class Class source
  Plain text file PdoPaginatorQuerySemanticsTest.php Class Class source
  Plain text file PdoPaginatorTransactionTest.php Class Class source

  Files folder image Files (76)  /  tests  /  Regression  
File Role Description
Files folder imageException (2 files)
Files folder imagePdo (2 directories)

  Files folder image Files (76)  /  tests  /  Regression  /  Exception  
File Role Description
  Plain text file InvalidOrderingCon...onExceptionTest.php Class Class source
  Plain text file PaginationExceptionContractTest.php Class Class source

  Files folder image Files (76)  /  tests  /  Regression  /  Pdo  
File Role Description
Files folder imageOrdering (1 file)
Files folder imagePagination (2 files)

  Files folder image Files (76)  /  tests  /  Regression  /  Pdo  /  Ordering  
File Role Description
  Plain text file OrderingPublicApiRegressionTest.php Class Class source

  Files folder image Files (76)  /  tests  /  Regression  /  Pdo  /  Pagination  
File Role Description
  Plain text file PaginationPublicApiRegressionTest.php Class Class source
  Plain text file PaginationSqlContractRegressionTest.php Class Class source

  Files folder image Files (76)  /  tests  /  Support  
File Role Description
Files folder imageMySql (9 files)
Files folder imagePdo (1 directory)

  Files folder image Files (76)  /  tests  /  Support  /  MySql  
File Role Description
  Plain text file MySqlConnectionFactory.php Class Class source
  Plain text file MySqlIntegrationTestCase.php Class Class source
  Plain text file OrderingFailureInjector.php Class Class source
  Plain text file OrderingFixture.php Class Class source
  Plain text file OrderingSchemaManager.php Class Class source
  Plain text file OrderingStateReader.php Class Class source
  Plain text file PaginationFixture.php Class Class source
  Plain text file PaginationIntegrationTestCase.php Class Class source
  Plain text file PaginationSchemaManager.php Class Class source

  Files folder image Files (76)  /  tests  /  Support  /  Pdo  
File Role Description
Files folder imagePagination (2 files)

  Files folder image Files (76)  /  tests  /  Support  /  Pdo  /  Pagination  
File Role Description
  Plain text file ScriptedPdo.php Class Class source
  Plain text file ScriptedPdoStatement.php Class Class source

  Files folder image Files (76)  /  tests  /  Unit  
File Role Description
Files folder imagePdo (2 directories)

  Files folder image Files (76)  /  tests  /  Unit  /  Pdo  
File Role Description
Files folder imageOrdering (1 file)
Files folder imagePagination (8 files)

  Files folder image Files (76)  /  tests  /  Unit  /  Pdo  /  Ordering  
File Role Description
  Plain text file ScopedOrderingConfigTest.php Class Class source

  Files folder image Files (76)  /  tests  /  Unit  /  Pdo  /  Pagination  
File Role Description
  Plain text file PageRequestTest.php Class Class source
  Plain text file PageResultTest.php Class Class source
  Plain text file PaginationConfigTest.php Class Class source
  Plain text file PdoPaginationQueryDescriptorTest.php Class Class source
  Plain text file PdoPaginatorExecutionTest.php Class Class source
  Plain text file PdoPaginatorFailureTest.php Class Class source
  Plain text file PdoPaginatorNormalizationTest.php Class Class source
  Plain text file SortWhitelistTest.php Class Class source

The PHP Classes site has supported package installation using the Composer tool since 2013, as you may verify by reading this instructions page.
Install with Composer Install with Composer
 Version Control Unique User Downloads  
 100%
Total:0
This week:0