stave.dev

Default Values

Three prefixes and plain values that GenericEntity::define() writes into the generated class's constructor.

The 'defaults' array of a GenericEntity::define() config maps an attribute name to a default. A default must be a string, number, boolean or null, and its key must be one of the declared attributes. A string that starts with a recognised prefix becomes matching PHP in the generated constructor; any other value is used as it is.

Defaults Live in the Generated File

Prefixes are turned into PHP once, when the class file is generated (GenericEntity::getClassTemplate()). Nothing reads the config again in later requests. To change a default afterwards, edit the generated file, or call define() again with $force = true.

Available Prefixes

Default Value given When to use
date:FORMAT date('FORMAT', time()) Created-at and any field that should default to "now" in a particular format.
uniqid:PREFIX uniqid('PREFIX') Tokens and reference ids distinct from the record's numeric id.
server:KEY $_SERVER['KEY'] ?? null Request-context fields such as HTTP_USER_AGENT. KEY must be upper-case letters, digits and underscores, or define() throws.
Any other string, a number, a boolean or null The value itself Fixed defaults like 'status' => 'active'.

A default is applied when its key is absent from the data array (array_key_exists()). A key that is present with a null value keeps the null.

Full Example

\Zero\Entity\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 this constructor:

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

    parent::__construct($data);
}

Creating a record now needs almost nothing at the call site:

new \Zero\Entity\SessionLog([]);  // all defaults apply — token, ip, user_agent, status, created_at
Defaults Are Data

Because the defaults fill the data array, new SessionLog() with no arguments stores a record. For an instance with no record, to call withId() or where() on, use SessionLog::getInstance(): it does not run the generated constructor. See Querying.

REMOTE_ADDR Behind a Proxy

server:REMOTE_ADDR is the connecting peer, which is the proxy when the site sits behind one. Core's Event class records \Zero\Core\Request::clientIp() instead; to do the same in a generated class, edit its constructor.

Edit the Constructor for Anything Else

The prefixes cover only the cases above. For conditional logic, lookups or transformations, edit the generated class's __construct by hand. define() will not overwrite the file unless you pass $force = true.