Help us shape the future of Go! Take the 2026 Go Developer Survey

orm

package
v1.20.0-alpha.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 3, 2026 License: MIT Imports: 9 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func EnforceMorphMap

func EnforceMorphMap(entries map[string]any, merge ...bool)

EnforceMorphMap is Relation::enforceMorphMap (Relation.php:459): RequireMorphMap(true) plus MorphMap(entries, merge...) — register the map and switch strict mode on in one call.

func GetID

func GetID(dest any) any

func GetIDByReflect

func GetIDByReflect(t reflect.Type, v reflect.Value) any

func IsIgnoringTouch

func IsIgnoringTouch(model any) bool

IsIgnoringTouch reports whether the given model's class ignores touches, mirroring Model::isIgnoringTouch: true for a model that carries no automatic updated-at column (Eloquent's `! get_class_vars($class)['timestamps'] || ! $class::UPDATED_AT`), and true while the class — or one of the classes it embeds, the Go form of is_subclass_of — sits in the ignore list a WithoutTouchingOn scope installed. A value that is not a model reports false.

func MorphAlias

func MorphAlias(model any) (string, bool)

MorphAlias returns the alias registered for the given model's underlying type. Useful at insert time when writing the value of a `*_type` column from a Go value.

func MorphMap

func MorphMap(entries map[string]any, merge ...bool)

MorphMap registers polymorphic aliases. Each entry maps an alias (the value stored in a `*_type` column) to a sample model instance from which the registry derives the underlying Go type.

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.

orm.MorphMap(map[string]any{
    "post":  &Post{},
    "video": &Video{},
})

Models that implement orm.ModelWithMorphClass take precedence over the registry.

func MorphedModel

func MorphedModel(alias string) any

MorphedModel returns a fresh pointer to a new instance of the model registered under alias, or nil if no model is registered. Used by the MorphTo loader to allocate the right Go type for a row whose `*_type` column contains alias.

func RequireMorphMap

func RequireMorphMap(requireMorphMap ...bool)

RequireMorphMap is Relation::requireMorphMap (Relation.php:437): it turns the process-level strict-morph flag on — or off when passed false. When on, resolving an unmapped model for a polymorphic `*_type` column raises errors.OrmClassMorphViolation instead of falling back to the model's table name, and a MorphTo associate raises when the associated model is unmapped. RequiresMorphMap reads the flag; nothing changes while it is off.

func RequiresMorphMap

func RequiresMorphMap() bool

RequiresMorphMap is Relation::requiresMorphMap (Relation.php:447): the strict-flag getter.

func WithoutTouching

func WithoutTouching(callback func() error) error

WithoutTouching runs callback with relationship touching disabled for every model: the Go form of Model::withoutTouching, which enters withoutTouchingOn([static::class]) for the base Model class — and with it every Go model that embeds Model, the way every Eloquent model extends the Eloquent base.

func WithoutTouchingOn

func WithoutTouchingOn(models []any, callback func() error) error

WithoutTouchingOn runs callback with relationship touching disabled for the given models' classes, mirroring Model::withoutTouchingOn: the classes join the process-wide ignore list for the duration of the callback and leave it again when the callback returns — an error or a panic included, the defer standing in for Eloquent's try / finally. The restore drops every occurrence of the entered classes, exactly as Eloquent's `array_diff(static::$ignoreOnTouch, $models)` does; a class entered by two nested scopes is therefore fully removed when the first of them ends, the Laravel behaviour.

Eloquent takes class names where the Go port takes sample model instances (the MorphMap convention): a class is derived from each value's struct type, and a value that is not a model is ignored.

Types

type DeletedAt

type DeletedAt struct {
	Time  time.Time
	Valid bool
}

DeletedAt records when a row was soft deleted. It is the type orm.SoftDeletes carries.

It is laid out exactly like database/sql.NullTime — Valid reports whether the row is soft deleted, and Time holds the moment it happened — so scanning and binding behave the same as the GORM type it replaces, and it can be converted to sql.NullTime rather than reimplementing the scanner.

The clause interfaces gorm.DeletedAt also implemented are deliberately absent. They were how GORM learned to filter soft-deleted rows and to turn a delete into an update; the framework now builds that predicate itself, so this type only has to carry the value.

func (DeletedAt) IsZero

func (r DeletedAt) IsZero() bool

IsZero reports whether no deletion time is recorded, which is how a soft-delete field is recognised as carrying a value.

func (DeletedAt) MarshalJSON

func (r DeletedAt) MarshalJSON() ([]byte, error)

MarshalJSON renders the deletion time, or null when the row was not soft deleted.

func (*DeletedAt) Scan

func (r *DeletedAt) Scan(value any) error

Scan implements sql.Scanner.

func (*DeletedAt) UnmarshalJSON

func (r *DeletedAt) UnmarshalJSON(data []byte) error

UnmarshalJSON restores the deletion time, reading null as "not soft deleted".

func (DeletedAt) Value

func (r DeletedAt) Value() (driver.Value, error)

Value implements driver.Valuer. A row that was not soft deleted stores NULL.

type Model

type Model struct {
	Timestamps
	ID uint `gorm:"primaryKey" json:"id"`
}

Model is the base model for all models in the application.

type SoftDeletes

type SoftDeletes struct {
	DeletedAt DeletedAt `gorm:"column:deleted_at" json:"deleted_at"`
}

SoftDeletes is used to add soft delete functionality to a model.

The field carries the framework's own type, so the ORM builds the soft-delete predicate and the delete rewrite itself instead of asking GORM to.

type Timestamps

type Timestamps struct {
	CreatedAt *carbon.DateTime `gorm:"autoCreateTime;column:created_at" json:"created_at"`
	UpdatedAt *carbon.DateTime `gorm:"autoUpdateTime;column:updated_at" json:"updated_at"`

	// Disabled mirrors Eloquent's per-instance $timestamps = false flag: while true, a write leaves
	// created_at and updated_at to the caller — a value the caller set is still written, an unset
	// column is left out of an insert so the database's own default applies. Go's zero value must
	// mean "timestamps on", so the flag is Eloquent's negated, hence the name.
	Disabled bool `gorm:"-" json:"-"`
}

Timestamps is used to add created_at and updated_at timestamps to a model.

func (Timestamps) UsesTimestamps

func (t Timestamps) UsesTimestamps() bool

UsesTimestamps reports whether the model instance stamps its automatic timestamps — the per-instance half of Eloquent's HasTimestamps::usesTimestamps, read from Disabled. Eloquent's other half, the suspension a withoutTimestamps scope installs, lives on the query in Go (Query.WithoutTimestamps); the query folds both when it decides whether a write stamps.

Directories

Path Synopsis
Package morphmap holds the process-wide registry of polymorphic aliases shared by the orm and gorm packages.
Package morphmap holds the process-wide registry of polymorphic aliases shared by the orm and gorm packages.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL