Documentation
¶
Overview ¶
Package morphmap holds the process-wide registry of polymorphic aliases shared by the orm and gorm packages. It mirrors fedaco's static Relation._morphMap (libs/fedaco/src/fedaco/ relations/relation.ts:31) while remaining safe for concurrent reads from many query goroutines.
Most app code interacts with the registry via the wrappers in package orm (orm.MorphMap, orm.MorphedModel, orm.MorphAlias, orm.RequireMorphMap, orm.RequiresMorphMap, orm.EnforceMorphMap). The lower-level gorm wrapper imports this package directly to resolve morph values during query construction without introducing a circular dependency on package orm.
Strict morph mode (Relation::requireMorphMap / requiresMorphMap / enforceMorphMap — Relation.php:437/447/459) is a process-level flag: when on, MorphValue raises errors.OrmClassMorphViolation for a model that resolves neither through ModelWithMorphClass nor the registry, instead of returning the not-found pair. The base relations.Pivot type stays exempt (HasRelationships.php:1014); the flag defaults to off.
Index ¶
- func AliasOf(model any) (string, bool)
- func All() map[string]reflect.Type
- func EnforceMorphMap(entries map[string]any, merge ...bool)
- func Find(alias string) any
- func MorphValue(model any) (string, bool, error)
- func Register(entries map[string]any, merge ...bool)
- func RequireMorphMap(requireMorphMap ...bool)
- func RequiresMorphMap() bool
- func Reset()
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func EnforceMorphMap ¶
EnforceMorphMap is Relation::enforceMorphMap (Relation.php:459): requireMorphMap(true) plus Register(entries, merge...) — registering the map and switching strict mode on in one call. PHP returns the resulting map; the Go registry's Register has no return, so neither does this.
func Find ¶
Find returns a fresh pointer to a new instance of the model registered under alias, or nil if no model is registered.
func MorphValue ¶
MorphValue resolves the morph class value for a model — the port of Eloquent's Model::getMorphClass (HasRelationships.php:1006). Resolution order:
- model.MorphClass() if the model (or its pointer) implements ModelWithMorphClass — an override replaces the whole resolution, exactly as a getMorphClass override does in PHP
- global morph map (registered via Register) — HasRelationships.php:1011
- the base relations.Pivot type — the `static::class === Pivot::class` carve-out (HasRelationships.php:1014): exempt from strict checking and resolved to its own class name
- strict mode (RequireMorphMap / EnforceMorphMap): any other unmapped model raises errors.OrmClassMorphViolation — the ClassMorphViolationException throw at HasRelationships.php:1019
Returns "" and false with a nil error when nothing resolves and strict mode is off. The caller is then expected to fall back to GORM's `polymorphicValue:` tag or the parent's table name (where PHP would return the class name — the documented divergence on orm.MorphAlias).
func Register ¶
Register stores a set of alias-to-sample-model bindings. Subsequent calls merge with previously registered entries; later writes win on conflict. Pass false in the optional merge argument to replace the registry instead of merging.
func RequireMorphMap ¶
func RequireMorphMap(requireMorphMap ...bool)
RequireMorphMap ports Relation::requireMorphMap (Relation.php:437): it turns the process-level strict-morph flag on — or off when passed false, the explicit form Laravel's $requireMorphMap parameter takes. The default is true, matching the PHP default argument.
func RequiresMorphMap ¶
func RequiresMorphMap() bool
RequiresMorphMap is Relation::requiresMorphMap (Relation.php:447): the getter half of the flag.
Types ¶
This section is empty.