Documentation
¶
Index ¶
- func EnforceMorphMap(entries map[string]any, merge ...bool)
- func GetID(dest any) any
- func GetIDByReflect(t reflect.Type, v reflect.Value) any
- func IsIgnoringTouch(model any) bool
- func MorphAlias(model any) (string, bool)
- func MorphMap(entries map[string]any, merge ...bool)
- func MorphedModel(alias string) any
- func RequireMorphMap(requireMorphMap ...bool)
- func RequiresMorphMap() bool
- func WithoutTouching(callback func() error) error
- func WithoutTouchingOn(models []any, callback func() error) error
- type DeletedAt
- type Model
- type SoftDeletes
- type Timestamps
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func EnforceMorphMap ¶
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 IsIgnoringTouch ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
IsZero reports whether no deletion time is recorded, which is how a soft-delete field is recognised as carrying a value.
func (DeletedAt) MarshalJSON ¶
MarshalJSON renders the deletion time, or null when the row was not soft deleted.
func (*DeletedAt) UnmarshalJSON ¶
UnmarshalJSON restores the deletion time, reading null as "not soft deleted".
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.