stave.dev

Default Values

Four default-value prefixes that GenericEntity::define() bakes into the generated constructor.

When you pass a 'defaults' array to GenericEntity::define(), each default is a string. If the string starts with a recognised prefix, the generator emits matching PHP into the generated class's constructor. Otherwise, the string is stored as a literal default.

Defaults Are Static, Not Dynamic

Prefixes are resolved into PHP code once, at generation time, and written into the constructor. They are not re-interpreted on each instantiation — edit the generated file if you need to change a default after the fact.

Available Prefixes

Prefix Expands to When to use
date:FORMAT date('FORMAT', time()) Created-at, updated-at, any field that should default to "now" formatted a particular way.
uniqid:PREFIX uniqid('PREFIX') Tokens, slugs, internal ids distinct from the row's numeric id.
server:KEY $_SERVER['KEY'] ?? null Request-context fields: REMOTE_ADDR, HTTP_USER_AGENT, etc.
(anything else) Literal string value Enum-ish defaults like status => 'active'.

Full Example

GenericEntity::define('session_log', [
    'attributes' => ['token', 'ip', 'user_agent', 'status', 'created_at'],
    'defaults'   => [
        'token'      => 'uniqid:sess_',
        'ip'         => 'server:REMOTE_ADDR',
        'user_agent' => 'server:HTTP_USER_AGENT',
        'status'     => 'active',
        'created_at' => 'date:Y-m-d H:i:s',
    ],
]);

The generator writes a constructor roughly equivalent to:

public function __construct($data = []) {
    $data['token']      ??= uniqid('sess_');
    $data['ip']         ??= $_SERVER['REMOTE_ADDR'] ?? null;
    $data['user_agent'] ??= $_SERVER['HTTP_USER_AGENT'] ?? null;
    $data['status']     ??= 'active';
    $data['created_at'] ??= date('Y-m-d H:i:s', time());
    parent::__construct($data);
}

Creating a row now requires almost nothing at the call site:

new \Zero\Entity\SessionLog([]);  // all defaults fire — ip, ua, status, token, created_at
Prefer Constructor Overrides for Anything Complex

The prefix system only covers the four cases above. If you need conditional logic, hashing, lookups, or transformations, override the generated class's __construct by hand — the generator won't overwrite it on re-runs unless you pass force: true.