Class: EntityFacade
Full name: Azera\Orm\Concerns\EntityFacade
The static facade surface that works on EVERY store, shared by the facade
base classes (Model, Document).
WHY A TRAIT: the two facades sit in separate hierarchies on purpose —
Document must NOT extend Model, because metadata PK resolution keys off
isSubclassOf(Model::class) to apply the SQL ['id'] chain, which would
break the document _id convention. Sharing the surface without sharing
the type keeps both the routing guarantee and the API parity.
WHY THESE METHODS ONLY: everything here goes through the EntityManager's
Store seam (find(), findBy(), persist(), upsert()), so it behaves
identically on SQL and on documents. The builder-based members
(query(), with(), fresh(), exists(), count()) compile SQL and are
therefore declared on Model alone, where Query refuses a non-SQL store
loudly instead of querying the wrong database.
Writes stay polymorphic: create(), firstOrCreate() and
updateOrCreate() call static::save(), so each facade keeps its own
insert/update policy.
Identity: a read hit returns the request-scoped heap instance, so a facade read and an EM read of the same row are the SAME object.
Public methods
find() ·
public static function find(mixed $id, bool $fresh = false): static|null
Find by its ID(s) through the EntityManager: heap probe first, one Store read on miss, hydration onto the shared heap. The returned instance is identity-mapped — the same row read twice in one request yields the same object.
$fresh=true re-reads the row and refreshes the tracked instance IN PLACE (same object, current values) — the polling-safe variant. Throws when the entity carries scheduled unflushed writes.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
$id |
mixed | - | Single ID value, or array of ID values (numeric list matching pkFields order, or field => value map for composite keys) |
$fresh |
bool | false |
Return value
- Type:
static|null
findOrFail() ·
public static function findOrFail(mixed $id, bool $fresh = false): static
Find by ID(s) or throw when nothing matches.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
$id |
mixed | - | Single ID value or array of ID values (composite keys) |
$fresh |
bool | false |
Re-read the row and refresh the tracked instance in place |
Return value
- Type:
static
Throws
- RuntimeException when not found
findOne() ·
public static function findOne(array $conditions, bool $fresh = false): static|null
Find the first match for the given conditions, or null. Conditions are field => value equality, same shape the Store seam accepts.
$fresh=true refreshes already-tracked hits in place (same instance, current values).
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
$conditions |
array | - | |
$fresh |
bool | false |
Return value
- Type:
static|null
findAll() ·
public static function findAll(array $conditions = [], bool $fresh = false): array
Find all matches for the given conditions (field => value equality), in store row order, as identity-mapped instances.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
$conditions |
array | [] |
|
$fresh |
bool | false |
Return value
- Type:
array
create() ·
public static function create(array $values): static
Build an instance from $values and persist it, returning it afterwards.
Flows through save(), so the class's own policy applies (an
auto-generated key is backfilled by the EntityManager).
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
$values |
array | - | field => value |
Return value
- Type:
static
upsert() ·
public static function upsert(array $values, Azera\Orm\ConflictTarget|null $on = null, array|null $updateColumns = null): static
Atomic UPSERT (INSERT ... ON CONFLICT DO UPDATE on SQL, updateOne with
upsert: true on documents): one statement, no prior SELECT — the
database resolves insert-vs-update, so there is no unique-violation
race.
By default every ID field must be present in $values, since the key is the conflict target. Pass $on to conflict on a DIFFERENT unique key — the form you need when the PK is database-generated or the real uniqueness lives in a separate index:
BillingCustomer::upsert(
['stripe_customer_id' => $id, 'email' => $email],
ConflictTarget::columns(['stripe_customer_id'], 'stripe_customer_id IS NOT NULL'),
);
On conflict, the non-target fields from $values are written — pass $updateColumns to narrow that on a wide row. With an explicit target, a null target value throws instead of inserting: it could never match, so the upsert would silently duplicate the row.
With an incomplete key (and no target) this degrades to a plain insert (there is no conflict target to promise).
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
$values |
array | - | field => value (must include all ID fields when $on is null) |
$on |
ConflictTarget|null | null |
key to conflict on; null = the ID fields |
$updateColumns |
array|null | null |
fields the conflict action may write; null = all non-target, [] = none |
Return value
- Type:
static
firstOrCreate() ·
public static function firstOrCreate(array $conditions, array $values = []): static
Return the first match for $conditions, or create one from $conditions merged with $values.
A read-then-write convenience, not a concurrency guarantee: two
callers racing the insert both miss and both create. Use
upsert() when the key is known and atomicity matters.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
$conditions |
array | - | field => value to match |
$values |
array | [] |
additional values for the new instance |
Return value
- Type:
static
updateOrCreate() ·
public static function updateOrCreate(array $conditions, array $values = []): static
Update the first match for $conditions with $values, or create one from $conditions merged with $values when there is no match.
Same read-then-write caveat as firstOrCreate().
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
$conditions |
array | - | field => value to match |
$values |
array | [] |
values to assign (or merge on create) |
Return value
- Type:
static