Model
class Model extends Extendable implements ArrayAccess, Arrayable, Jsonable, JsonSerializable
Model class.
Traits
Adds event related features to any class.
Extendable Trait Allows for "Private traits"
Properties
protected array | $extensionData | from ExtendableTrait | |
static protected array | $extendableCallbacks | from ExtendableTrait | |
static protected array | $extendableStaticMethods | from ExtendableTrait | |
static protected bool | $extendableGuardProperties | from ExtendableTrait | |
static protected ClassLoader | $extendableClassLoader | from ExtendableTrait | |
$implement | from Extendable | ||
$hidden | The attributes that should be hidden for serialization. |
from HidesAttributes | |
protected array | $visible | The attributes that should be visible in serialization. |
from HidesAttributes |
array | $attributes | The model's attributes. |
from HasAttributes |
protected array | $original | The model attribute's original state. |
from HasAttributes |
protected array | $changes | The changed model attributes. |
from HasAttributes |
static bool | $snakeAttributes | Indicates whether attributes are snake cased on arrays. |
from HasAttributes |
static protected array | $mutatorCache | The cache of the mutated attributes for each class. |
from HasAttributes |
protected array | $fillable | The attributes that are mass assignable. |
from GuardsAttributes |
protected array | $dispatchesEvents | The event map for the model. |
from HasEvents |
protected array | $observables | User exposed observable events. |
from HasEvents |
static protected CacheManager | $cache | The cache manager instance. |
from ManagesCache |
protected bool | $loadedFromCache | from ManagesCache | |
protected array | $emitterSingleEvents | from EventEmitter | |
protected array | $emitterEvents | from EventEmitter | |
protected array | $emitterEventSorted | from EventEmitter | |
static protected SourceResolverInterface | $resolver | The source resolver instance. |
|
static | $dispatcher | ||
static protected array | $booted | The array of booted models. |
|
static protected array | $eventsBooted | The array of booted events. |
|
protected | $source | ||
protected string | $dirName | ||
protected array | $appends | The accessors to append to the model's array form. |
|
protected array | $purgeable | ||
bool | $exists | Indicates if the model exists. |
|
protected array | $allowedExtensions | ||
protected string | $defaultExtension | ||
protected int | $maxNesting |
Methods
Helper method for ::extend() static method
Clear the list of extended classes so they will be re-extended.
Normalizes the provided extension name allowing for the ClassLoader to inject aliased classes
Dynamically extend a class with a specified behavior
Extracts the available methods from a behavior and adds it to the list of callable methods.
Programmatically adds a method to the extendable class
Programatically adds a property to the extendable class
Check if extendable class is extended with a behavior object
Returns a behavior object from an extendable class, example:
Short hand for getClassExtension() method, except takes the short extension name, example:
Checks if a method exists, extension equivalent of method_exists()
Get a list of class methods, extension equivalent of get_class_methods()
Checks if a property exists, extension equivalent of property_exists()
Checks if a property is accessible, property equivalent of is_callabe()
Magic method for __call()
Magic method for __callStatic()
Create a new Halcyon model instance.
Dynamically retrieve attributes on the model.
Dynamically set attributes on the model.
Handle dynamic method calls into the model.
Handle dynamic static method calls into the method.
Add hidden attributes for the model.
Add visible attributes for the model.
Make the given, typically hidden, attributes visible.
Make the given, typically visible, attributes hidden.
Add the mutated attributes to the attributes array.
Get an attribute array of all arrayable attributes.
Get an attribute array of all arrayable values.
Get an attribute from the $attributes array.
Determine if a get mutator exists for an attribute.
Get the value of an attribute using its mutator.
Get the value of an attribute using its mutator for array conversion.
Set a given attribute on the model.
Determine if a set mutator exists for an attribute.
Set the array of model attributes. No checking is done.
Get the model's original attribute values.
Sync a single original attribute with its current value.
Determine if the model or given attribute(s) have been modified.
Determine if the model or given attribute(s) have remained the same.
Determine if the model or given attribute(s) have been modified.
Determine if the given attributes were changed.
Determine if the new and old values for a given key are equivalent.
Extract and cache all the mutated attributes of a class.
Get all of the attribute mutator methods.
Get the fillable attributes of a given array.
Determine if the given attribute may be mass assigned.
Remove an observable event name.
Register a model event with the dispatcher.
Bind some nicer events to this model, in the format of method overrides.
Fire the given event for the model.
Fire a custom model event for the given event.
Register a saving model event with the dispatcher.
Register a saved model event with the dispatcher.
Register an updating model event with the dispatcher.
Register an updated model event with the dispatcher.
Register a creating model event with the dispatcher.
Register a created model event with the dispatcher.
Register a deleting model event with the dispatcher.
Register a deleted model event with the dispatcher.
Set the event dispatcher instance.
Initializes the object properties from the cached data. The extra data set here becomes available as attributes set on the model after fetch.
Returns true if the object was loaded from the cache.
Create a new event binding.
Create a new event binding that fires once only
Sort the listeners for a given event by priority.
Fire an event and call the listeners.
Fires a combination of local and global events. The first segment is removed from the event name locally and the local object is passed as the first argument to the event globally. Halting is also enabled by default.
Check if the model needs to be booted and if so, do it.
The "booting" method of the model.
Boot all of the bootable traits on the model.
Clear the list of booted models so they will be re-booted.
No description
Get the source resolver instance.
Unset the source resolver for models.
Create a collection of models from plain arrays.
Fill the model with an array of attributes.
No description
Get the current source name for the model.
Returns the file name without the extension.
The settings is attribute contains everything that should be saved to the settings area.
Filling the settings should merge it with attributes.
File name should always contain an extension.
Returns the directory name corresponding to the object type.
Returns the allowable file extensions supported by this model.
Returns the maximum directory nesting allowed by this template.
Returns the base file name and extension. Applies a default extension, if none found.
Create a new Collection instance.
Create a new instance of the given model.
Create a new model instance that is existing.
Update the model in the database.
Save the model to the source.
Save the model to the database. Is used by save()} and {@link forceSave().
Finish processing on a successful save operation.
Delete the model from the database.
Perform the actual delete query on this model instance.
Convert the model to its string representation.
Determine if an attribute exists on the model.
Unset an attribute on the model.
Determine if the given attribute exists.
Get the value for a given offset.
Set the value for a given offset.
Unset the value for a given offset.
Get the instance as an array.
Convert the model instance to JSON.
Convert the object into something JSON serializable.
Details
in
ExtendableTrait at line 55
extendableConstruct()
Constructor.
in
ExtendableTrait at line 103
static void
extendableExtendCallback(callable $callback)
Helper method for ::extend() static method
in
ExtendableTrait at line 120
static void
clearExtendedClasses()
Clear the list of extended classes so they will be re-extended.
in
ExtendableTrait at line 131
protected string
extensionNormalizeClassName(string $name)
Normalizes the provided extension name allowing for the ClassLoader to inject aliased classes
in
ExtendableTrait at line 149
void|ExtendableTrait
extendClassWith(string $extensionName)
Dynamically extend a class with a specified behavior
in
ExtendableTrait at line 178
protected void
extensionExtractMethods(string $extensionName, object $extensionObject)
Extracts the available methods from a behavior and adds it to the list of callable methods.
in
ExtendableTrait at line 206
addDynamicMethod(string $dynamicName, callable $method, string $extension = null)
Programmatically adds a method to the extendable class
in
ExtendableTrait at line 225
addDynamicProperty(string $dynamicName, string $value = null)
Programatically adds a property to the extendable class
in
ExtendableTrait at line 249
bool
isClassExtendedWith(string $name)
Check if extendable class is extended with a behavior object
in
ExtendableTrait at line 265
mixed
getClassExtension(string $name)
Returns a behavior object from an extendable class, example:
$this->getClassExtension('Admin.Actions.FormController')
in
ExtendableTrait at line 280
mixed
asExtension(string $shortName)
Short hand for getClassExtension() method, except takes the short extension name, example:
$this->asExtension('FormController')
in
ExtendableTrait at line 301
bool
methodExists(string $name)
Checks if a method exists, extension equivalent of method_exists()
in
ExtendableTrait at line 313
array
getClassMethods()
Get a list of class methods, extension equivalent of get_class_methods()
in
ExtendableTrait at line 326
array
getDynamicProperties()
Returns all dynamic properties and their values
in
ExtendableTrait at line 344
bool
propertyExists(string $name)
Checks if a property exists, extension equivalent of property_exists()
in
ExtendableTrait at line 370
protected bool
extendableIsAccessible(mixed $class, string $propertyName)
Checks if a property is accessible, property equivalent of is_callabe()
in
ExtendableTrait at line 385
string
extendableGet(string $name)
Magic method for __get()
in
ExtendableTrait at line 410
string
extendableSet(string $name, string $value)
Magic method for __set()
in
ExtendableTrait at line 444
mixed
extendableCall(string $name, array $params = null)
Magic method for __call()
in
ExtendableTrait at line 483
static mixed
extendableCallStatic(string $name, array $params = null)
Magic method for __callStatic()
in
ExtendableTrait at line 552
protected ClassLoader|null
extensionGetClassLoader()
Gets the class loader
at line 98
__construct(array $attributes = [])
Create a new Halcyon model instance.
at line 712
__get(string $key)
Dynamically retrieve attributes on the model.
at line 725
__set(string $key, $value)
Dynamically set attributes on the model.
at line 738
__call(string $method, array $parameters)
Handle dynamic method calls into the model.
at line 758
static
__callStatic(string $method, array $parameters)
Handle dynamic static method calls into the method.
in
Extendable at line 50
static
extend(callable $callback)
No description
in
HidesAttributes at line 26
array
getHidden()
Get the hidden attributes for the model.
in
HidesAttributes at line 38
$this
setHidden(array $hidden)
Set the hidden attributes for the model.
in
HidesAttributes at line 52
void
addHidden(array|string|null $attributes = null)
Add hidden attributes for the model.
in
HidesAttributes at line 64
array
getVisible()
Get the visible attributes for the model.
in
HidesAttributes at line 76
$this
setVisible(array $visible)
Set the visible attributes for the model.
in
HidesAttributes at line 90
void
addVisible(array|string|null $attributes = null)
Add visible attributes for the model.
in
HidesAttributes at line 104
$this
makeVisible(array|string $attributes)
Make the given, typically hidden, attributes visible.
in
HidesAttributes at line 122
$this
makeHidden(array|string $attributes)
Make the given, typically visible, attributes hidden.
in
HasAttributes at line 51
array
attributesToArray()
Convert the model's attributes to an array.
in
HasAttributes at line 77
protected array
addMutatedAttributesToArray(array $attributes, array $mutatedAttributes)
Add the mutated attributes to the attributes array.
in
HasAttributes at line 103
protected array
getArrayableAttributes()
Get an attribute array of all arrayable attributes.
in
HasAttributes at line 113
protected array
getArrayableAppends()
Get all of the appendable values that are arrayable.
in
HasAttributes at line 131
protected array
getArrayableItems(array $values)
Get an attribute array of all arrayable values.
in
HasAttributes at line 151
mixed
getAttribute(string $key)
Get an attribute from the model.
in
HasAttributes at line 187
mixed
getAttributeValue(string $key)
Get a plain attribute (not a relationship).
in
HasAttributes at line 208
protected mixed
getAttributeFromArray(string $key)
Get an attribute from the $attributes array.
in
HasAttributes at line 222
bool
hasGetMutator(string $key)
Determine if a get mutator exists for an attribute.
in
HasAttributes at line 235
protected mixed
mutateAttribute(string $key, mixed $value)
Get the value of an attribute using its mutator.
in
HasAttributes at line 248
protected mixed
mutateAttributeForArray(string $key, mixed $value)
Get the value of an attribute using its mutator for array conversion.
in
HasAttributes at line 263
$this
setAttribute(string $key, mixed $value)
Set a given attribute on the model.
in
HasAttributes at line 286
bool
hasSetMutator(string $key)
Determine if a set mutator exists for an attribute.
in
HasAttributes at line 296
array
getAttributes()
Get all of the current attributes on the model.
in
HasAttributes at line 309
$this
setRawAttributes(array $attributes, bool $sync = false)
Set the array of model attributes. No checking is done.
in
HasAttributes at line 328
mixed|array
getOriginal(string|null $key = null, mixed $default = null)
Get the model's original attribute values.
in
HasAttributes at line 340
array
only(array|mixed $attributes)
Get a subset of the model's attributes.
in
HasAttributes at line 356
$this
syncOriginal()
Sync the original attributes with the current.
in
HasAttributes at line 370
$this
syncOriginalAttribute(string $attribute)
Sync a single original attribute with its current value.
in
HasAttributes at line 382
$this
syncChanges()
Sync the changed attributes.
in
HasAttributes at line 396
bool
isDirty(array|string|null $attributes = null)
Determine if the model or given attribute(s) have been modified.
in
HasAttributes at line 410
bool
isClean(array|string|null $attributes = null)
Determine if the model or given attribute(s) have remained the same.
in
HasAttributes at line 422
bool
wasChanged(array|string|null $attributes = null)
Determine if the model or given attribute(s) have been modified.
in
HasAttributes at line 437
protected bool
hasChanges(array $changes, array|string|null $attributes = null)
Determine if the given attributes were changed.
in
HasAttributes at line 463
array
getDirty()
Get the attributes that have been changed since last sync.
in
HasAttributes at line 481
array
getChanges()
Get the attributes that was changed.
in
HasAttributes at line 494
protected bool
originalIsEquivalent(string $key, mixed $current)
Determine if the new and old values for a given key are equivalent.
in
HasAttributes at line 518
array
getMutatedAttributes()
Get the mutated attributes for a given instance.
in
HasAttributes at line 536
static void
cacheMutatedAttributes(string $class)
Extract and cache all the mutated attributes of a class.
in
HasAttributes at line 550
static protected array
getMutatorMethods(mixed $class)
Get all of the attribute mutator methods.
in
GuardsAttributes at line 21
array
getFillable()
Get the fillable attributes for the model.
in
GuardsAttributes at line 33
$this
fillable(array $fillable)
Set the fillable attributes for the model.
in
GuardsAttributes at line 47
protected array
fillableFromArray(array $attributes)
Get the fillable attributes of a given array.
in
GuardsAttributes at line 65
bool
isFillable(string $key)
Determine if the given attribute may be mass assigned.
in
HasEvents at line 34
static void
observe(object|string $class)
Register an observer with the Model.
in
HasEvents at line 55
array
getObservableEvents()
Get the observable event names.
in
HasEvents at line 74
$this
setObservableEvents(array $observables)
Set the observable event names.
in
HasEvents at line 88
void
addObservableEvents(array|mixed $observables)
Add an observable event name.
in
HasEvents at line 102
void
removeObservableEvents(array|mixed $observables)
Remove an observable event name.
in
HasEvents at line 118
static protected void
registerModelEvent(string $event, Closure|string $callback, int $priority = 0)
Register a model event with the dispatcher.
in
HasEvents at line 130
protected
bootNicerEvents()
Bind some nicer events to this model, in the format of method overrides.
in
HasEvents at line 177
protected mixed
fireModelEvent(string $event, bool $halt = true)
Fire the given event for the model.
in
HasEvents at line 209
protected mixed|null
fireCustomModelEvent(string $event, string $method)
Fire a custom model event for the given event.
in
HasEvents at line 229
protected mixed
filterModelEventResults(mixed $result)
Filter the model event results.
in
HasEvents at line 247
static void
retrieving(Closure|string $callback)
Create a new native event for handling beforeFetch().
in
HasEvents at line 259
static void
retrieved(Closure|string $callback)
Register a retrieved model event with the dispatcher.
in
HasEvents at line 272
static void
saving(Closure|string $callback, int $priority = 0)
Register a saving model event with the dispatcher.
in
HasEvents at line 285
static void
saved(Closure|string $callback, int $priority = 0)
Register a saved model event with the dispatcher.
in
HasEvents at line 298
static void
updating(Closure|string $callback, int $priority = 0)
Register an updating model event with the dispatcher.
in
HasEvents at line 311
static void
updated(Closure|string $callback, int $priority = 0)
Register an updated model event with the dispatcher.
in
HasEvents at line 324
static void
creating(Closure|string $callback, int $priority = 0)
Register a creating model event with the dispatcher.
in
HasEvents at line 337
static void
created(Closure|string $callback, int $priority = 0)
Register a created model event with the dispatcher.
in
HasEvents at line 350
static void
deleting(Closure|string $callback, int $priority = 0)
Register a deleting model event with the dispatcher.
in
HasEvents at line 363
static void
deleted(Closure|string $callback, int $priority = 0)
Register a deleted model event with the dispatcher.
in
HasEvents at line 373
static void
flushEventListeners()
Remove all of the event listeners for the model.
in
HasEvents at line 391
static Dispatcher
getEventDispatcher()
Get the event dispatcher instance.
in
HasEvents at line 403
static void
setEventDispatcher(Dispatcher $dispatcher)
Set the event dispatcher instance.
in
HasEvents at line 413
static void
unsetEventDispatcher()
Unset the event dispatcher for models.
in
ManagesCache at line 22
static CacheManager
getCacheManager()
Get the cache manager instance.
in
ManagesCache at line 34
static void
setCacheManager(CacheManager $cache)
Set the cache manager instance.
in
ManagesCache at line 43
static void
unsetCacheManager()
Unset the cache manager for models.
in
ManagesCache at line 54
static
initCacheItem($item)
Initializes the object properties from the cached data. The extra data set here becomes available as attributes set on the model after fetch.
in
ManagesCache at line 62
bool
isLoadedFromCache()
Returns true if the object was loaded from the cache.
in
ManagesCache at line 74
void
setLoadedFromCache($value)
Returns true if the object was loaded from the cache.
in
EventEmitter at line 36
EventEmitter
bindEvent(string $event, callable $callback, int $priority = 0)
Create a new event binding.
in
EventEmitter at line 52
EventEmitter
bindEventOnce(string $event, callable $callback)
Create a new event binding that fires once only
in
EventEmitter at line 66
protected void
emitterEventSortEvents(string $eventName)
Sort the listeners for a given event by priority.
in
EventEmitter at line 84
EventEmitter
unbindEvent(string $event = null)
Destroys an event binding.
in
EventEmitter at line 124
string|array
fireEvent(string $event, array $params = [], bool $halt = false)
Fire an event and call the listeners.
in
EventEmitter at line 178
mixed
fireSystemEvent(string $event, array $params = [], bool $halt = true)
Fires a combination of local and global events. The first segment is removed from the event name locally and the local object is passed as the first argument to the event globally. Halting is also enabled by default.
For example:
$this->fireSystemEvent('admin.form.myEvent', ['my value']);
Is equivalent to:
$this->fireEvent('form.myEvent', ['myvalue'], true);
Event::fire('admin.form.myEvent', [$this, 'myvalue'], true);
at line 116
protected void
bootIfNotBooted()
Check if the model needs to be booted and if so, do it.
at line 134
static protected void
boot()
The "booting" method of the model.
at line 144
static protected void
bootTraits()
Boot all of the bootable traits on the model.
at line 160
static void
clearBootedModels()
Clear the list of booted models so they will be re-booted.
at line 184
static SourceInterface
resolveSource(string|null $source = null)
No description
at line 193
static SourceResolverInterface
getSourceResolver()
Get the source resolver instance.
at line 205
static void
setSourceResolver(SourceResolverInterface $resolver)
Set the source resolver instance.
at line 214
static void
unsetSourceResolver()
Unset the source resolver for models.
at line 227
static Collection
hydrate(array $items, string|null $source = null)
Create a collection of models from plain arrays.
at line 246
static Model
create(array $attributes = [])
Save a new model and return the instance.
at line 260
static Finder
query()
Begin querying the model.
at line 270
static Collection|Model[]
all()
Get all of the models from the source.
at line 286
$this
fill(array $attributes)
Fill the model with an array of attributes.
at line 300
SourceInterface
getSource()
No description
at line 310
Model
setSource($source)
No description
at line 321
string
getSourceName()
Get the current source name for the model.
at line 330
string
getBaseFileNameAttribute()
Returns the file name without the extension.
at line 345
array
getSettingsAttribute()
The settings is attribute contains everything that should be saved to the settings area.
at line 364
setSettingsAttribute(mixed $value)
Filling the settings should merge it with attributes.
at line 375
setFileNameAttribute(mixed $value)
File name should always contain an extension.
at line 391
string
getTypeDirName()
Returns the directory name corresponding to the object type.
For pages the directory name is "_pages", for layouts - "_layouts", etc.
at line 400
array
getAllowedExtensions()
Returns the allowable file extensions supported by this model.
at line 409
int
getMaxNesting()
Returns the maximum directory nesting allowed by this template.
at line 421
array
getFileNameParts(string $fileName = null)
Returns the base file name and extension. Applies a default extension, if none found.
at line 450
Finder
newFinder()
Get a new file finder for the object
at line 466
Collection
newCollection(array $models = [])
Create a new Collection instance.
at line 479
Model
newInstance(array $attributes = [], bool $exists = false)
Create a new instance of the given model.
at line 499
Model
newFromFinder(array $attributes = [], string|null $source = null)
Create a new model instance that is existing.
at line 519
bool|int
update(array $attributes = [])
Update the model in the database.
at line 535
bool
save(array $options = [])
Save the model to the source.
at line 547
bool
saveInternal(array $options = [])
Save the model to the database. Is used by save()} and {@link forceSave().
at line 584
protected void
finishSave(array $options)
Finish processing on a successful save operation.
at line 601
protected bool
performUpdate(Finder $query, array $options = [])
Perform a model update operation.
at line 633
protected bool
performInsert(Finder $query, array $options = [])
Perform a model insert operation.
at line 662
bool|null
delete()
Delete the model from the database.
at line 691
protected void
performDeleteOnModel()
Perform the actual delete query on this model instance.
at line 700
string
__toString()
Convert the model to its string representation.
at line 772
bool
__isset(string $key)
Determine if an attribute exists on the model.
at line 788
void
__unset(string $key)
Unset an attribute on the model.
at line 800
bool
offsetExists(mixed $offset)
Determine if the given attribute exists.
at line 812
mixed
offsetGet(mixed $offset)
Get the value for a given offset.
at line 825
void
offsetSet(mixed $offset, mixed $value)
Set the value for a given offset.
at line 837
void
offsetUnset(mixed $offset)
Unset the value for a given offset.
at line 846
array
toArray()
Get the instance as an array.
at line 858
string
toJson(int $options = 0)
Convert the model instance to JSON.
at line 868
array
jsonSerialize()
Convert the object into something JSON serializable.