Skip to content

Repository files navigation

Hand-drawn Ray.MediaQuery logo showing sky, sea, and land separated by horizon and shoreline boundaries.

Ray.MediaQuery

codecovType CoverageContinuous Integration

Interface-Driven SQL for PHP

Ray.MediaQuery lets SQL be SQL and objects be objects.

Define a PHP interface, attach #[DbQuery], write a SQL file, and Ray.MediaQuery provides the implementation through Ray.Di + AOP. Return types and docblocks drive fetching, hydration, pagination, and post-query result objects.

useRay\MediaQuery\Annotation\DbQuery;
interface UserQueryInterface
{
#[DbQuery('user_item')]
publicfunctionitem(string$id): ?User;
}
finalclass User
{
publicfunction__construct(
publicreadonlystring$id,
publicreadonlystring$name,
) {}
}
-- sql/user_item.sqlSELECT id, name FROM users WHERE id = :id;
$userQuery = $injector->getInstance(UserQueryInterface::class);
$user = $userQuery->item('user-123');

Why Ray.MediaQuery?

  • Zero implementation code — interfaces become working query objects.
  • SQL-first — use joins, CTEs, window functions, vendor-specific SQL, and query plans directly.
  • Typed PHP results — hydrate rows to entities, typed collections, or custom result objects.
  • Rich domain objects — use factory: classes, including DI-aware factories, to create computed or service-backed objects, such as exposing age from a stored birth_date.
  • Explicit boundaries — SQL files, PHP interfaces, and domain objects remain visible and testable.
  • AI-friendly — no hidden query generation; the contract is readable by humans and tools.

Installation

composer require ray/media-query

Quick Start

useRay\AuraSqlModule\AuraSqlModule;
useRay\Di\AbstractModule;
useRay\Di\Injector;
useRay\MediaQuery\Annotation\DbQuery;
useRay\MediaQuery\MediaQuerySqlModule;
finalclass AppModule extends AbstractModule
{
protectedfunctionconfigure(): void
{
$this->install(newMediaQuerySqlModule(
interfaceDir: __DIR__ . '/Query',
sqlDir: __DIR__ . '/sql',
));
$this->install(newAuraSqlModule('sqlite::memory:'));
}
}
interface TodoQueryInterface
{
#[DbQuery('todo_add')]
publicfunctionadd(string$id, string$title): void;
/** @return array<Todo> */
#[DbQuery('todo_list')]
publicfunctionlist(): array;
}
$injector = newInjector(newAppModule());
$todoQuery = $injector->getInstance(TodoQueryInterface::class);
$todoQuery->add('todo-1', 'Write SQL');
$todos = $todoQuery->list();

Result Types at a Glance

DeclarationMeaning
arrayList of associative rows
?array + type: 'row'Single associative row or null
/** @return array<User> */ arrayHydrated entity list
?User + type: 'row'Single hydrated entity or null
voidExecute DML and ignore the result
AffectedRowsDML row count
InsertedRowINSERT id and resolved bound values
Pages<User>Lazy paginated hydrated rows
PostQueryInterfaceCustom post-query result object

Documentation

Start from the Documentation Home. It is the single entry point for the manual, hands-on tutorial, BDR pattern, FAQ, ecosystem links, and AI-oriented reference.

Demo Application

See demo/ for a minimal runnable smoke test of the module wiring. The hands-on tutorial in the documentation site is the full feature walkthrough.

Philosophy

Ray.MediaQuery does not hide SQL to make objects comfortable, and it does not flatten objects to make SQL convenient. It lets both sides do what they are good at: SQL expresses data access precisely, while PHP expresses types, domain behavior, and dependency-injected object construction.

About

A media access mapping framework

Resources

Contributing

Security policy

Stars

11 stars

Watchers

3 watching

Forks

Releases

Packages

Used by

Contributors

Languages