Skip to content

Internals Models

Stefan Crkvenjakov edited this page Oct 7, 2026 · 1 revision

Models internals

Framework Internals · Developer guide

Model extends DatabaseManager and adds primary-key state, declared/dynamic attributes and ORM snapshots. selectByID resolves table/key defaults, fetches one row and clears prior hydration state; hydrateRow assigns attributes, establishes table/key/id, resolves relations with recursion protection and syncs originals.

Persistence collects initialized public properties and dynamicData, excludes internal/blacklisted/relation fields, optionally intersects writableAttributes, serializes JSON casts/backed enums, and updates/inserts through DatabaseManager. fill uses explicit fillableAttributes; fromArray is trusted assignment. hiddenAttributes affects output only. Casts include int/float/string/bool/json; unsupported casts, invalid booleans or internal-property assignment fail.

save requires model_id, excludes the key from update, adds a timestamp for registered updated_on, and updates all allowed fields. saveModel inserts and sets the key; deleteModel rejects missing key and clears it after success. Exceptions are wrapped in ModelCRUDException/ModelCreationException. getModels clones/hydrates already-fetched rows; firstModelOrFail throws for no result. newQuery preserves connection/cache and resets builder/attributes.

Relations are configured through with/belongsTo/hasMany interfaces, not generic dynamic relationship methods. The row's foreign key or local key determines related lookup. Default relation loading shares the connection before lookup; inConstructor modes can perform constructor queries first. Single missing relations become null; has-many populates arrays. Lazy __get supports the with alias, caches unknown missing fields as null and prevents accidental persistence of those misses.

ModelCollection immediately selects all rows, obtains the concrete model class, creates clones using hydrateRow, and wraps them in an ObjectCollection. It implements IteratorAggregate/Countable and avoids repeat lookup queries on normal hydration. It is not a lazy database cursor or paginated query API.

Component reference

Clone this wiki locally