Runo

Runo — Records & query classes

Every row is hydrated into RunoSupportRecord unless another class is specified. Use cast() on a statement or $casts on a query class to convert values.

Records

$result = DB::prepare('SELECT id, title FROM posts')->execute();
$post = $result->first();

echo $post->id;
echo $post->title;

$attributes = $post->toArray();

Record supports magic access (__get/__set/__isset). Assigning properties only changes the in-memory object (no save()/update()/delete()).

Casting values

use Runo\DB;

$result = DB::prepare(
    'SELECT id, is_published, score, tags, created_at FROM posts'
)->cast([
    'id'           => 'int',
    'is_published' => 'bool',
    'score'        => 'float',
    'tags'         => 'json',
    'created_at'   => DateTimeImmutable::class,
])->execute();

$post = $result->first();
// $post->id (int), $post->is_published (bool), $post->score (float),
// $post->tags (array), $post->created_at (DateTimeImmutable)
CastBehavior
'int'Casts with (int)
'float'Casts with (float)
'bool'Casts with (bool) (note: string "false" becomes true)
'json'If string, json_decode($value, true); invalid JSON becomes null
DateTimeInterface classnew $class($value)

Casts are only applied when the column is present and not null. Missing cast strings are ignored.

Custom record classes

use Runo\Support\Record;

final class Post extends Record
{
    public function excerpt(int $length = 100): string
    {
        return substr((string) $this->title, 0, $length);
    }
}
use Runo\DB;

$result = DB::prepare('SELECT id, title FROM posts')
    ->as(Post::class)
    ->cast(['id' => 'int'])
    ->execute();

$post = $result->first();
echo get_class($post); // Post
echo $post->excerpt(40);

The class must extend Runo\Support\Record, otherwise Hydrator throws Exception("Invalid record class.").

Class-level casts

A record class's $casts is overwritten by the query's cast array by default; merge them in the constructor if you want defaults that can be overridden:

final class CastedPost extends Record
{
    protected array $casts = [
        'id'         => 'int',
        'created_at' => DateTimeImmutable::class,
    ];

    public function __construct(array|object $attributes = [], array $casts = [])
    {
        parent::__construct($attributes, array_merge($this->casts, $casts));
    }
}

Reusable query classes

use Runo\Database\Query;

final class PublishedPosts extends Query
{
    protected array $casts = ['id' => 'int'];
    protected static ?string $recordClass = Post::class;

    public function sql(): string
    {
        return 'SELECT id, title, created_at
                FROM posts
                WHERE status = :status
                ORDER BY id DESC';
    }
}
$posts = PublishedPosts::execute([':status' => 'published']);
// or
$query = new PublishedPosts([':status' => 'draft']);
$posts = $query->run();

Query::run() passes SQL, args, casts, and record class to DB::prepare(). If your subclass overrides the constructor it must accept array $args = [] for Query::execute().

Combining Query with Sql

Sql bindings are not propagated automatically—assign $this->args = $query->args() inside sql():

use Runo\Database\Query;
use Runo\Support\Sql;

final class ActivePosts extends Query
{
    protected array $casts = ['id' => 'int'];

    public function sql(): string
    {
        $query = Sql::table('posts')
            ->select(['id', 'title'])
            ->where('status', '=', 'published')
            ->orderBy('id', 'DESC');

        $this->args = $query->args();
        return $query->toString();
    }
}

Part of the Lunaris documentation · Source on Codeberg · MIT License

Part of the Lunaris documentation · Source on Codeberg · MIT License