1. Go to this page and download the library: Download maatify/persistence library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
maatify / persistence example snippets
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,
);
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
);