Configuration
tenant()
Signature
Section titled “Signature”tenant() — returns struct
Available in: controller, model, mapper, migrator, migration, tabledefinition
Category: Multi-Tenancy
Description
Section titled “Description”Returns the current tenant struct, or an empty struct if no tenant is active.
The tenant struct contains: id, dataSource, config, and $locked.
A tenant only counts as active when it carries a non-empty dataSource — the same test
$tenantDataSource() applies before it routes a query. Anything else on the key reads as
no tenant rather than being handed back as though it were a resolved one, so a malformed
value degrades to a no-op instead of wrong behaviour (#3336). Every framework producer
(switchTenant(), TenantResolver, Job.$restoreTenantContext(), TenantMigrator)
already guarantees a non-empty dataSource, so this only filters foreign values.
Examples
Section titled “Examples”// 1. Get the active tenant struct (when a tenant is set)
t = tenant();
// t -> {id: "acme", dataSource: "tenant_db_acme", config: {}, $locked: true}
// 2. Check whether a tenant is active before using its properties
t = tenant();
if (!structIsEmpty(t)) {
writeOutput("Current tenant: " & t.id);
} else {
writeOutput("No tenant active — using application defaults.");
}
// 3. Access a per-tenant config value set via switchTenant()
t = tenant();
if (structKeyExists(t, "config") && structKeyExists(t.config, "timeZone")) {
writeOutput("Tenant time zone: " & t.config.timeZone);
}