Download the PHP package detain/dbrel-data-php without Composer
On this page you can find all versions of the php package detain/dbrel-data-php. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download detain/dbrel-data-php
More information about detain/dbrel-data-php
Files in detain/dbrel-data-php
Package dbrel-data-php
Short Description PHP backend data provider for database relationship visualization. Collects customer data across multiple databases and computes relationship matches for consumption by dbrel-viz.
License MIT
Homepage https://github.com/detain/dbrel-data-php
Informations about the package dbrel-data-php
TL;DR — Give it a JSON schema describing your tables and how they relate, pass it a database handle, and it produces a visualization-ready payload. No ORM, no AST parsing, no code generation.
Table of Contents
- Why dbrel-data-php?
- Features
- Installation
- Quick Start
- Schema JSON Format
- API Reference
- Relationship Types
- Integration Example
- Adapting Your Database Layer
- Architecture
- Companion Packages
- Requirements
- Contributing
- License
Why dbrel-data-php?
Information-schema joins tell you about foreign keys. Your application knows about everything else — the FIND_IN_SET column that binds hosts to groups, the cross-database references between your primary DB and your helpdesk DB, the polymorphic module_id column that points at different tables depending on a module_type value.
dbrel-data-php lets you describe those relationships in one JSON file, then computes the actual row matches between them so your frontend can draw edges without ever looking at the raw data itself.
Features
- JSON-defined schema — one file describes your modules, prefixes, primary keys, and every kind of relationship
- Three relationship types out of the box —
directforeign-key matches,find_in_setCSV column bridges,cross_dbcross-database refs - Works with any database layer — implement a 5-method
DbInterfacearound your existingmysqli/PDO/custom wrapper - Pivot filtering — re-center the payload on a specific table/row and auto-trim to tables within 2 hops
- Row-level match arrays — not just "
accountslinks tovps", but exactly which rows link to which - Virtual tables — synthesize tables from pivoted rows (e.g.
accounts_extbuilt from a key/valueaccounts_extratable) - Metadata baked in — query time, table / row counts, database list, pivot state
- Zero framework lock-in — vanilla PHP 7.4+ with PSR-4 autoloading, no Laravel/Symfony assumptions
- Stable JSON output — byte-compatible with the Node.js sister package
dbrel-data-js
Installation
Then require Composer's autoloader and use the classes:
Quick Start
A minimal endpoint that returns the payload for a given customer:
Hand that JSON to dbrel-viz and you're done.
Schema JSON Format
The schema is a single JSON file with these top-level keys. The library only strictly requires relationships; everything else is for metadata and display.
Relationship-object fields (click to expand)
| Field | Type | Required | Description | | :-- | :-- | :-: | :-- | | `source_db` | `string` | yes | Logical database name, e.g. `"my"` | | `source_table` | `string` | yes | Source table name | | `source_field` | `string` | yes | Column on the source holding the reference | | `target_db` | `string` | yes | Logical database name of the target | | `target_table` | `string` | yes | Target table name | | `target_field` | `string` | yes | Column on the target being referenced | | `type` | `string` | no | `direct` \| `find_in_set` \| `cross_db` (default `direct`) | | `cardinality` | `string` | no | `1:1` \| `1:N` \| `N:1` \| `N:M` (default `1:N`) | | `label` | `string` | no | Human-readable label for tooltips | | `notes` | `string` | no | Free-text notes — stored but unused by the matcher | Unknown types (e.g. `code_join`, `fk_constraint`) are normalized to `direct`. Relationships whose `target_table` starts with `(` (polymorphic placeholders like `(vps|websites)`) are silently skipped.API Reference
DbRel\Data\RelationshipSchema
Loads and normalizes the relationship schema JSON.
| Method | Returns | Description |
|---|---|---|
__construct(string\|array $jsonPathOrArray) |
— | Accepts a file path or a pre-decoded array. Throws InvalidArgumentException on bad input. |
getRules(): array |
array |
Normalized relationship rules (ready for the matcher). |
getModules(): array |
array |
Module definitions keyed by module name. |
getTableToModule(): array |
array |
Lookup map: table name → module name. |
getVirtualTables(): array |
array |
Virtual table definitions (e.g. pivot-synthesized tables). |
getMetadata(): array |
array |
The _metadata block from the JSON. |
getRaw(): array |
array |
The raw decoded JSON. |
DbRel\Data\DataCollector
Accumulates rows from SQL queries into a normalized structure.
| Method | Description |
|---|---|
collect(DbInterface $db, string $dbName, string $table, string $sql, int $limit = 50): void |
Runs $sql and captures up to $limit rows. Records full $total. |
addTable(string $dbName, string $table, array $rows, array $columns, ?int $total = null): void |
Manually register a table (for virtual tables, caches, etc). |
appendRows(string $dbName, string $table, array $rows): void |
Append rows to an existing table or create it. |
getTables(): array |
Every collected table, keyed by "db.table". |
has(string $key): bool |
Whether a given "db.table" key exists. |
getRows(string $key): array |
Rows for a given key (empty if missing). |
getTotalRows(): int |
Sum of total across all tables. |
DbRel\Data\RelationshipMatcher
Given collected data and schema rules, computes row-level match arrays.
| Method | Description |
|---|---|
compute(array $tablesData, array $rules): array |
Returns the active relationships, each with a matches array of [sourceRowIdx, [targetRowIdxs]]. |
Algorithmic behavior:
direct/cross_db— strict string-equality match onsource_fieldvalue vs.target_fieldvaluefind_in_set— source field is split on,and compared to target field byin_array()- Source values of
null,'','0'are skipped to avoid noise (you rarely want to connect every row with a zero FK) - Only relationships with at least one match are returned
DbRel\Data\DataProvider
Ties everything together and emits the final payload.
| Method | Description |
|---|---|
__construct(RelationshipSchema $schema, ?RelationshipMatcher $matcher = null) |
Instantiate. Records a start time for query_time_ms. |
build(DataCollector $collector, array $options = []): array |
Produces the payload. |
getSchema(): RelationshipSchema |
Accessor. |
getMatcher(): RelationshipMatcher |
Accessor. |
build() options (click to expand)
| Key | Type | Default | Description |
| :-- | :-- | :-: | :-- |
| `custid` | `int` | `0` | Customer/entity ID — echoed into metadata |
| `primaryKeys` | `arrayResponse shape (click to expand)
DbRel\Data\DbInterface
A 5-method contract your database wrapper must satisfy.
If you use MyAdmin's \MyDb\Mysqli\Db, it already matches — no adapter needed. For other stacks see Adapting Your Database Layer.
Relationship Types
The schema supports several type values. Internally the matcher collapses them down to three behaviors.
direct
Classic FK join. Cheap, O(N×M) with early-exit on null/zero/empty source values.
find_in_set
Source column is CSV. The matcher splits it on ,, trims each token, and checks each target row's value against the set.
cross_db
Same matching logic as direct, but the frontend draws the edge in a different style so you can see DB boundaries at a glance.
Integration Example
Drop-in AJAX endpoint using a procedural entry point:
Point dbrel-viz's ajaxUrl at this script and you have a working, swappable-renderer database explorer.
Adapting Your Database Layer
You only need to implement the 5-method DbInterface. Example wrapping PDO:
Collectors expect DbInterface::next_record() to leave the current row on a $db->Record public property — this matches the MyAdmin and PHPLIB tradition. If your wrapper returns rows differently, either set $this->Record in next_record() (as above) or expose getRecord(): array which DataCollector will call via method_exists().
Architecture
Companion Packages
This package is half of a two-package system:
| Package | Language | Purpose |
|---|---|---|
@detain/dbrel-viz |
Browser JS | Frontend library — 20 pluggable renderers |
detain/dbrel-data-php |
PHP ≥ 7.4 | This package — the backend |
@detain/dbrel-data-js |
Node ≥ 14 | Same API, for Node/Express stacks |
Payload parity — dbrel-data-php and dbrel-data-js emit byte-identical JSON given the same schema and data. Swap backends freely; the frontend won't notice.
Requirements
- PHP ≥ 7.4 (tested on 7.4, 8.0, 8.1, 8.2, 8.3)
- ext-mysqli (for the default
DbInterfaceshape — or adapt to PDO, see above) - ext-json
- MySQL or MariaDB — any version supporting the queries you write
Contributing
PR guidelines
- One feature or fix per PR
- New relationship types go on
RelationshipMatcher; schema format is frozen - All public APIs must carry PHPDoc
- PHPUnit tests for any behavior change
- PSR-12 code style
Ideas we'd love help with
- PostgreSQL adapter — the matcher is DB-agnostic, but we don't ship adapters yet
- Schema generator — scan
information_schemaand build the JSON automatically - Caching layer — the matcher is deterministic, so
compute()results can be cached - Symfony / Laravel integration packages
License
Joe Huss / InterServer
All versions of dbrel-data-php with dependencies
ext-json Version *
ext-mysqli Version *