| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454 |
- <?php
- declare(strict_types=1);
- /**
- * This file is part of Hyperf.
- *
- * @link https://www.hyperf.io
- * @document https://hyperf.wiki
- * @contact group@hyperf.io
- * @license https://github.com/hyperf/hyperf/blob/master/LICENSE
- */
- namespace Hyperf\Database\Model;
- use ArrayAccess;
- use Exception;
- use Hyperf\Collection\Arr;
- use Hyperf\Collection\Collection as BaseCollection;
- use Hyperf\Contract\Arrayable;
- use Hyperf\Contract\CompressInterface;
- use Hyperf\Contract\Jsonable;
- use Hyperf\Contract\UnCompressInterface;
- use Hyperf\Database\ConnectionInterface;
- use Hyperf\Database\Model\Relations\Pivot;
- use Hyperf\Database\Query\Builder as QueryBuilder;
- use Hyperf\Stringable\Str;
- use Hyperf\Stringable\StrCache;
- use JsonSerializable;
- use Psr\EventDispatcher\EventDispatcherInterface;
- use Psr\EventDispatcher\StoppableEventInterface;
- use Throwable;
- use function Hyperf\Collection\collect;
- use function Hyperf\Collection\last;
- use function Hyperf\Support\class_basename;
- use function Hyperf\Support\class_uses_recursive;
- use function Hyperf\Tappable\tap;
- /**
- * @mixin ModelIDE
- */
- abstract class Model implements ArrayAccess, Arrayable, Jsonable, JsonSerializable, CompressInterface
- {
- use Concerns\HasAttributes;
- use Concerns\HasEvents;
- use Concerns\HasGlobalScopes;
- use Concerns\HasRelationships;
- use Concerns\HasTimestamps;
- use Concerns\HidesAttributes;
- use Concerns\GuardsAttributes;
- /**
- * The name of the "created at" column.
- *
- * @var null|string
- */
- public const CREATED_AT = 'created_at';
- /**
- * The name of the "updated at" column.
- *
- * @var null|string
- */
- public const UPDATED_AT = 'updated_at';
- /**
- * Indicates if the IDs are auto-incrementing.
- */
- public bool $incrementing = true;
- /**
- * Indicates if the model exists.
- */
- public bool $exists = false;
- /**
- * Indicates if the model was inserted during the current request lifecycle.
- */
- public bool $wasRecentlyCreated = false;
- protected array $guarded = ['*'];
- /**
- * The connection name for the model.
- */
- protected ?string $connection = 'default';
- /**
- * The table associated with the model.
- */
- protected ?string $table = null;
- /**
- * The primary key for the model.
- */
- protected string $primaryKey = 'id';
- /**
- * The "type" of the auto-incrementing ID.
- */
- protected string $keyType = 'int';
- /**
- * The relations to eager load on every query.
- */
- protected array $with = [];
- /**
- * The relationship counts that should be eager loaded on every query.
- */
- protected array $withCount = [];
- /**
- * The number of models to return for pagination.
- */
- protected int $perPage = 15;
- /**
- * The array of trait initializers that will be called on each new instance.
- */
- protected array $traitInitializers = [];
- /**
- * Create a new Model model instance.
- */
- public function __construct(array $attributes = [])
- {
- $this->bootIfNotBooted();
- $this->initializeTraits();
- $this->syncOriginal();
- $this->fill($attributes);
- }
- /**
- * Dynamically retrieve attributes on the model.
- *
- * @param string $key
- */
- public function __get($key)
- {
- return $this->getAttribute($key);
- }
- /**
- * Dynamically set attributes on the model.
- *
- * @param string $key
- * @param mixed $value
- */
- public function __set($key, $value)
- {
- $this->setAttribute($key, $value);
- }
- /**
- * Determine if an attribute or relation exists on the model.
- *
- * @param string $key
- * @return bool
- */
- public function __isset($key)
- {
- return $this->offsetExists($key);
- }
- /**
- * Unset an attribute on the model.
- *
- * @param string $key
- */
- public function __unset($key)
- {
- $this->offsetUnset($key);
- }
- /**
- * Handle dynamic method calls into the model.
- *
- * @param string $method
- * @param array $parameters
- */
- public function __call($method, $parameters)
- {
- if (in_array($method, ['increment', 'decrement'])) {
- return $this->{$method}(...$parameters);
- }
- if ($resolver = $this->relationResolver(static::class, $method)) {
- return $resolver($this);
- }
- return $this->newQuery()->{$method}(...$parameters);
- }
- /**
- * Handle dynamic static method calls into the method.
- *
- * @param string $method
- * @param array $parameters
- */
- public static function __callStatic($method, $parameters)
- {
- return (new static())->{$method}(...$parameters);
- }
- /**
- * Convert the model to its string representation.
- */
- public function __toString(): string
- {
- return $this->toJson();
- }
- /**
- * Prepare the object for serialization.
- *
- * @return array
- */
- public function __sleep()
- {
- $this->mergeAttributesFromClassCasts();
- $this->classCastCache = [];
- return array_keys(get_object_vars($this));
- }
- /**
- * When a model is being unserialized, check if it needs to be booted.
- */
- public function __wakeup()
- {
- $this->bootIfNotBooted();
- }
- /**
- * Disables relationship model touching for the current class during given callback scope.
- */
- public static function withoutTouching(callable $callback)
- {
- static::withoutTouchingOn([static::class], $callback);
- }
- /**
- * Disables relationship model touching for the given model classes during given callback scope.
- */
- public static function withoutTouchingOn(array $models, callable $callback)
- {
- IgnoreOnTouch::$container = array_values(array_merge(IgnoreOnTouch::$container, $models));
- try {
- $callback();
- } finally {
- IgnoreOnTouch::$container = array_values(array_diff(IgnoreOnTouch::$container, $models));
- }
- }
- /**
- * Determine if the given model is ignoring touches.
- *
- * @param null|string $class
- * @return bool
- */
- public static function isIgnoringTouch($class = null)
- {
- $class = $class ?: static::class;
- foreach (IgnoreOnTouch::$container as $ignoredClass) {
- if ($class === $ignoredClass || is_subclass_of($class, $ignoredClass)) {
- return true;
- }
- }
- return false;
- }
- /**
- * Fill the model with an array of attributes.
- *
- * @return $this
- * @throws MassAssignmentException
- */
- public function fill(array $attributes)
- {
- $totallyGuarded = $this->totallyGuarded();
- foreach ($this->fillableFromArray($attributes) as $key => $value) {
- $key = $this->removeTableFromKey($key);
- // The developers may choose to place some attributes in the "fillable" array
- // which means only those attributes may be set through mass assignment to
- // the model, and all others will just get ignored for security reasons.
- if ($this->isFillable($key)) {
- $this->setAttribute($key, $value);
- } elseif ($totallyGuarded) {
- throw new MassAssignmentException(sprintf('Add [%s] to fillable property to allow mass assignment on [%s].', $key, get_class($this)));
- }
- }
- return $this;
- }
- /**
- * Fill the model with an array of attributes. Force mass assignment.
- *
- * @return $this
- */
- public function forceFill(array $attributes)
- {
- return static::unguarded(function () use ($attributes) {
- return $this->fill($attributes);
- });
- }
- /**
- * Qualify the given column name by the model's table.
- *
- * @param string $column
- * @return string
- */
- public function qualifyColumn($column)
- {
- if (Str::contains($column, '.')) {
- return $column;
- }
- return $this->getTable() . '.' . $column;
- }
- /**
- * Create a new instance of the given model.
- *
- * @param array $attributes
- * @param bool $exists
- * @return static
- */
- public function newInstance($attributes = [], $exists = false)
- {
- // This method just provides a convenient way for us to generate fresh model
- // instances of this current model. It is particularly useful during the
- // hydration of new objects via the Model query builder instances.
- $model = new static((array) $attributes);
- $model->exists = $exists;
- $model->setConnection($this->getConnectionName());
- $model->setTable($this->getTable());
- $model->mergeCasts($this->casts);
- return $model;
- }
- /**
- * Create a new model instance that is existing.
- *
- * @param array $attributes
- * @param null|string $connection
- * @return static
- */
- public function newFromBuilder($attributes = [], $connection = null)
- {
- $model = $this->newInstance([], true);
- $model->setRawAttributes((array) $attributes, true);
- $model->setConnection($connection ?: $this->getConnectionName());
- $model->fireModelEvent('retrieved');
- return $model;
- }
- /**
- * Begin querying the model on a given connection.
- *
- * @param null|string $connection
- * @return Builder
- */
- public static function on($connection = null)
- {
- // First we will just create a fresh instance of this model, and then we can set the
- // connection on the model so that it is used for the queries we execute, as well
- // as being set on every relation we retrieve without a custom connection name.
- $instance = new static();
- $instance->setConnection($connection);
- return $instance->newQuery();
- }
- /**
- * Begin querying the model on the write connection.
- *
- * @return QueryBuilder
- */
- public static function onWriteConnection()
- {
- return static::query()->useWritePdo();
- }
- /**
- * Get all of the models from the database.
- *
- * @param array|mixed $columns
- * @return Collection|static[]
- */
- public static function all($columns = ['*'])
- {
- return static::query()->get(is_array($columns) ? $columns : func_get_args());
- }
- /**
- * Begin querying a model with eager loading.
- *
- * @param array|string $relations
- * @return Builder|static
- */
- public static function with($relations)
- {
- return static::query()->with(is_string($relations) ? func_get_args() : $relations);
- }
- /**
- * Eager load relations on the model.
- *
- * @param array|string $relations
- * @return $this
- */
- public function load($relations)
- {
- $query = $this->newQueryWithoutRelationships()->with(is_string($relations) ? func_get_args() : $relations);
- $query->eagerLoadRelations([$this]);
- return $this;
- }
- /**
- * Eager load relationships on the polymorphic relation of a model.
- *
- * @param string $relation
- * @param array $relations
- * @return $this
- */
- public function loadMorph($relation, $relations)
- {
- $className = get_class($this->{$relation});
- $this->{$relation}->load($relations[$className] ?? []);
- return $this;
- }
- /**
- * Eager load relations on the model if they are not already eager loaded.
- *
- * @param array|string $relations
- * @return $this
- */
- public function loadMissing($relations)
- {
- $relations = is_string($relations) ? func_get_args() : $relations;
- $this->newCollection([$this])->loadMissing($relations);
- return $this;
- }
- /**
- * Eager load relation counts on the model.
- *
- * @param array|string $relations
- * @return $this
- */
- public function loadCount($relations)
- {
- $relations = is_string($relations) ? func_get_args() : $relations;
- $this->newCollection([$this])->loadCount($relations);
- return $this;
- }
- /**
- * Eager load relationship counts on the polymorphic relation of a model.
- *
- * @param string $relation
- * @param array $relations
- * @return $this
- */
- public function loadMorphCount($relation, $relations)
- {
- $className = get_class($this->{$relation});
- $this->{$relation}->loadCount($relations[$className] ?? []);
- return $this;
- }
- /**
- * Update the model in the database.
- *
- * @return bool
- */
- public function update(array $attributes = [], array $options = [])
- {
- if (! $this->exists) {
- return false;
- }
- return $this->fill($attributes)->save($options);
- }
- /**
- * Save the model and all of its relationships.
- *
- * @return bool
- */
- public function push()
- {
- if (! $this->save()) {
- return false;
- }
- // To sync all of the relationships to the database, we will simply spin through
- // the relationships and save each model via this "push" method, which allows
- // us to recurse into all of these nested relations for the model instance.
- foreach ($this->relations as $models) {
- $models = $models instanceof Collection ? $models->all() : [$models];
- foreach (array_filter($models) as $model) {
- if (! $model->push()) {
- return false;
- }
- }
- }
- return true;
- }
- /**
- * Save the model to the database.
- */
- public function save(array $options = []): bool
- {
- $this->mergeAttributesFromClassCasts();
- $query = $this->newModelQuery();
- // If the "saving" event returns false we'll bail out of the save and return
- // false, indicating that the save failed. This provides a chance for any
- // listeners to cancel save operations if validations fail or whatever.
- if ($saving = $this->fireModelEvent('saving')) {
- if ($saving instanceof StoppableEventInterface && $saving->isPropagationStopped()) {
- return false;
- }
- }
- // If the model already exists in the database we can just update our record
- // that is already in this database using the current IDs in this "where"
- // clause to only update this model. Otherwise, we'll just insert them.
- if ($this->exists) {
- $saved = $this->isDirty() ? $this->performUpdate($query) : true;
- } else {
- // If the model is brand new, we'll insert it into our database and set the
- // ID attribute on the model to the value of the newly inserted row's ID
- // which is typically an auto-increment value managed by the database.
- $saved = $this->performInsert($query);
- if (! $this->getConnectionName() && $connection = $query->getConnection()) {
- $this->setConnection($connection->getName());
- }
- }
- // If the model is successfully saved, we need to do a few more things once
- // that is done. We will call the "saved" method here to run any actions
- // we need to happen after a model gets successfully saved right here.
- if ($saved) {
- $this->finishSave($options);
- }
- return $saved;
- }
- /**
- * Save the model to the database using transaction.
- *
- * @return bool
- * @throws Throwable
- */
- public function saveOrFail(array $options = [])
- {
- return $this->getConnection()->transaction(function () use ($options) {
- return $this->save($options);
- });
- }
- /**
- * Destroy the models for the given IDs.
- *
- * @param array|BaseCollection|int $ids
- */
- public static function destroy($ids): int
- {
- // We'll initialize a count here so we will return the total number of deletes
- // for the operation. The developers can then check this number as a boolean
- // type value or get this total count of records deleted for logging, etc.
- $count = 0;
- if ($ids instanceof BaseCollection) {
- $ids = $ids->all();
- }
- $ids = is_array($ids) ? $ids : func_get_args();
- // We will actually pull the models from the database table and call delete on
- // each of them individually so that their events get fired properly with a
- // correct set of attributes in case the developers wants to check these.
- $key = ($instance = new static())->getKeyName();
- foreach ($instance->whereIn($key, $ids)->get() as $model) {
- if ($model->delete()) {
- ++$count;
- }
- }
- return $count;
- }
- /**
- * Delete the model from the database.
- *
- * @return null|bool
- * @throws Exception
- */
- public function delete()
- {
- $this->mergeAttributesFromClassCasts();
- if (is_null($this->getKeyName())) {
- throw new Exception('No primary key defined on model.');
- }
- // If the model doesn't exist, there is nothing to delete so we'll just return
- // immediately and not do anything else. Otherwise, we will continue with a
- // deletion process on the model, firing the proper events, and so forth.
- if (! $this->exists) {
- return;
- }
- if ($event = $this->fireModelEvent('deleting')) {
- if ($event instanceof StoppableEventInterface && $event->isPropagationStopped()) {
- return false;
- }
- }
- // Here, we'll touch the owning models, verifying these timestamps get updated
- // for the models. This will allow any caching to get broken on the parents
- // by the timestamp. Then we will go ahead and delete the model instance.
- $this->touchOwners();
- $this->performDeleteOnModel();
- // Once the model has been deleted, we will fire off the deleted event so that
- // the developers may hook into post-delete operations. We will then return
- // a boolean true as the delete is presumably successful on the database.
- $this->fireModelEvent('deleted');
- return true;
- }
- /**
- * Force a hard delete on a soft deleted model.
- * This method protects developers from running forceDelete when trait is missing.
- *
- * @return null|bool
- */
- public function forceDelete()
- {
- return $this->delete();
- }
- /**
- * Begin querying the model.
- *
- * @return Builder
- */
- public static function query()
- {
- return (new static())->newQuery();
- }
- /**
- * Get a new query builder for the model's table.
- *
- * @return Builder
- */
- public function newQuery()
- {
- return $this->registerGlobalScopes($this->newQueryWithoutScopes());
- }
- /**
- * Get a new query builder that doesn't have any global scopes or eager loading.
- *
- * @return Builder|static
- */
- public function newModelQuery()
- {
- return $this->newModelBuilder($this->newBaseQueryBuilder())->setModel($this);
- }
- /**
- * Get a new query builder with no relationships loaded.
- *
- * @return Builder
- */
- public function newQueryWithoutRelationships()
- {
- return $this->registerGlobalScopes($this->newModelQuery());
- }
- /**
- * Register the global scopes for this builder instance.
- *
- * @param Builder $builder
- * @return Builder
- */
- public function registerGlobalScopes($builder)
- {
- foreach ($this->getGlobalScopes() as $identifier => $scope) {
- $builder->withGlobalScope($identifier, $scope);
- }
- return $builder;
- }
- /**
- * Get a new query builder that doesn't have any global scopes.
- *
- * @return Builder|static
- */
- public function newQueryWithoutScopes()
- {
- return $this->newModelQuery()->with($this->with)->withCount($this->withCount);
- }
- /**
- * Get a new query instance without a given scope.
- *
- * @param Scope|string $scope
- * @return Builder
- */
- public function newQueryWithoutScope($scope)
- {
- return $this->newQuery()->withoutGlobalScope($scope);
- }
- /**
- * Get a new query to restore one or more models by their queueable IDs.
- *
- * @param array|int $ids
- * @return Builder
- */
- public function newQueryForRestoration($ids)
- {
- return is_array($ids) ? $this->newQueryWithoutScopes()
- ->whereIn($this->getQualifiedKeyName(), $ids) : $this->newQueryWithoutScopes()->whereKey($ids);
- }
- /**
- * Create a new Model query builder for the model.
- *
- * @param QueryBuilder $query
- * @return Builder|static
- */
- public function newModelBuilder($query)
- {
- return new Builder($query);
- }
- /**
- * Create a new Model Collection instance.
- *
- * @return Collection
- */
- public function newCollection(array $models = [])
- {
- return new Collection($models);
- }
- /**
- * Create a new pivot model instance.
- *
- * @param string $table
- * @param bool $exists
- * @param null|string $using
- * @return Pivot
- */
- public function newPivot(self $parent, array $attributes, $table, $exists, $using = null)
- {
- return $using ? $using::fromRawAttributes($parent, $attributes, $table, $exists) : Pivot::fromAttributes($parent, $attributes, $table, $exists);
- }
- /**
- * Convert the model instance to an array.
- */
- public function toArray(): array
- {
- return array_merge($this->attributesToArray(), $this->relationsToArray());
- }
- /**
- * Convert the model instance to JSON.
- *
- * @param int $options
- * @return string
- * @throws JsonEncodingException
- */
- public function toJson($options = 0)
- {
- $json = json_encode($this->jsonSerialize(), $options);
- if (json_last_error() !== JSON_ERROR_NONE) {
- throw JsonEncodingException::forModel($this, json_last_error_msg());
- }
- return $json;
- }
- /**
- * Convert the object into something JSON serializable.
- */
- public function jsonSerialize(): mixed
- {
- return $this->toArray();
- }
- /**
- * Reload a fresh model instance from the database.
- *
- * @param array|string $with
- * @return null|static
- */
- public function fresh($with = [])
- {
- if (! $this->exists) {
- return;
- }
- return static::newQueryWithoutScopes()
- ->with(is_string($with) ? func_get_args() : $with)
- ->where($this->getKeyName(), $this->getKey())
- ->first();
- }
- /**
- * Reload the current model instance with fresh attributes from the database.
- *
- * @return $this
- */
- public function refresh()
- {
- if (! $this->exists) {
- return $this;
- }
- $this->setRawAttributes(static::newQueryWithoutScopes()->findOrFail($this->getKey())->attributes);
- $this->load(collect($this->relations)->except('pivot')->keys()->toArray());
- $this->syncOriginal();
- return $this;
- }
- /**
- * Clone the model into a new, non-existing instance.
- *
- * @return static
- */
- public function replicate(?array $except = null)
- {
- $defaults = [
- $this->getKeyName(),
- $this->getCreatedAtColumn(),
- $this->getUpdatedAtColumn(),
- ];
- $attributes = Arr::except($this->getAttributes(), $except ? array_unique(array_merge($except, $defaults)) : $defaults);
- return tap(new static(), function ($instance) use ($attributes) {
- // @var \Hyperf\Database\Model\Model $instance
- $instance->setRawAttributes($attributes);
- $instance->setRelations($this->relations);
- });
- }
- /**
- * Determine if two models have the same ID and belong to the same table.
- *
- * @param null|Model $model
- * @return bool
- */
- public function is($model)
- {
- return ! is_null($model) && $this->getKey() === $model->getKey() && $this->getTable() === $model->getTable() && $this->getConnectionName() === $model->getConnectionName();
- }
- /**
- * Determine if two models are not the same.
- *
- * @param null|Model $model
- * @return bool
- */
- public function isNot($model)
- {
- return ! $this->is($model);
- }
- /**
- * Get the database connection for the model.
- * You can write it by yourself.
- */
- public function getConnection(): ConnectionInterface
- {
- return Register::resolveConnection($this->getConnectionName());
- }
- /**
- * Get the event dispatcher for the model.
- * You can write it by yourself.
- */
- public function getEventDispatcher(): ?EventDispatcherInterface
- {
- return Register::getEventDispatcher();
- }
- /**
- * Get the current connection name for the model.
- *
- * @return string
- */
- public function getConnectionName()
- {
- return $this->connection;
- }
- /**
- * Set the connection associated with the model.
- *
- * @param string $name
- * @return $this
- */
- public function setConnection($name)
- {
- $this->connection = $name;
- return $this;
- }
- /**
- * Get the table associated with the model.
- */
- public function getTable(): string
- {
- return $this->table ?? StrCache::snake(Str::pluralStudly(class_basename($this)));
- }
- /**
- * Set the table associated with the model.
- *
- * @param string $table
- * @return $this
- */
- public function setTable($table)
- {
- $this->table = $table;
- return $this;
- }
- /**
- * Get the primary key for the model.
- *
- * @return string
- */
- public function getKeyName()
- {
- return $this->primaryKey;
- }
- /**
- * Set the primary key for the model.
- *
- * @param string $key
- * @return $this
- */
- public function setKeyName($key)
- {
- $this->primaryKey = $key;
- return $this;
- }
- /**
- * Get the table qualified key name.
- *
- * @return string
- */
- public function getQualifiedKeyName()
- {
- return $this->qualifyColumn($this->getKeyName());
- }
- /**
- * Get the auto-incrementing key type.
- *
- * @return string
- */
- public function getKeyType()
- {
- return $this->keyType;
- }
- /**
- * Set the data type for the primary key.
- *
- * @param string $type
- * @return $this
- */
- public function setKeyType($type)
- {
- $this->keyType = $type;
- return $this;
- }
- /**
- * Get the value indicating whether the IDs are incrementing.
- *
- * @return bool
- */
- public function getIncrementing()
- {
- return $this->incrementing;
- }
- /**
- * Set whether IDs are incrementing.
- *
- * @param bool $value
- * @return $this
- */
- public function setIncrementing($value)
- {
- $this->incrementing = $value;
- return $this;
- }
- /**
- * Get the value of the model's primary key.
- */
- public function getKey()
- {
- return $this->getAttribute($this->getKeyName());
- }
- /**
- * Get the value of the model's route key.
- */
- public function getRouteKey()
- {
- return $this->getAttribute($this->getRouteKeyName());
- }
- /**
- * Get the route key for the model.
- *
- * @return string
- */
- public function getRouteKeyName()
- {
- return $this->getKeyName();
- }
- /**
- * Retrieve the model for a bound value.
- *
- * @param mixed $value
- * @return null|Model
- */
- public function resolveRouteBinding($value)
- {
- return $this->where($this->getRouteKeyName(), $value)->first();
- }
- /**
- * Get the default foreign key name for the model.
- *
- * @return string
- */
- public function getForeignKey()
- {
- return StrCache::snake(class_basename($this)) . '_' . $this->getKeyName();
- }
- /**
- * Get the number of models to return per page.
- */
- public function getPerPage(): int
- {
- return $this->perPage;
- }
- /**
- * Set the number of models to return per page.
- *
- * @param int $perPage
- * @return $this
- */
- public function setPerPage($perPage)
- {
- $this->perPage = $perPage;
- return $this;
- }
- /**
- * Determine if the given attribute exists.
- */
- public function offsetExists(mixed $offset): bool
- {
- return ! is_null($this->getAttribute($offset));
- }
- /**
- * Get the value for a given offset.
- */
- public function offsetGet(mixed $offset): mixed
- {
- return $this->getAttribute($offset);
- }
- /**
- * Set the value for a given offset.
- */
- public function offsetSet(mixed $offset, mixed $value): void
- {
- $this->setAttribute($offset, $value);
- }
- /**
- * Unset the value for a given offset.
- */
- public function offsetUnset(mixed $offset): void
- {
- unset($this->attributes[$offset], $this->relations[$offset]);
- }
- public function compress(): UnCompressInterface
- {
- $key = $this->getKey();
- $class = get_class($this);
- return new ModelMeta($class, $key);
- }
- /**
- * Check if the model needs to be booted and if so, do it.
- */
- protected function bootIfNotBooted(): void
- {
- $booted = Booted::$container[static::class] ?? false;
- if (! $booted) {
- Booted::$container[static::class] = true;
- $this->fireModelEvent('booting');
- $this->boot();
- $this->fireModelEvent('booted');
- }
- }
- /**
- * The "booting" method of the model.
- */
- protected function boot(): void
- {
- $this->bootTraits();
- }
- /**
- * Boot all of the bootable traits on the model.
- */
- protected function bootTraits(): void
- {
- $class = static::class;
- $booted = [];
- TraitInitializers::$container[$class] = [];
- foreach (class_uses_recursive($class) as $trait) {
- $method = 'boot' . class_basename($trait);
- if (method_exists($class, $method) && ! in_array($method, $booted)) {
- forward_static_call([$class, $method]);
- $booted[] = $method;
- }
- if (method_exists($class, $method = 'initialize' . class_basename($trait))) {
- TraitInitializers::$container[$class][] = $method;
- TraitInitializers::$container[$class] = array_unique(TraitInitializers::$container[$class]);
- }
- }
- }
- /**
- * Remove the table name from a given key.
- */
- protected function removeTableFromKey(string $key): string
- {
- return Str::contains($key, '.') ? last(explode('.', $key)) : $key;
- }
- /**
- * Increment a column's value by a given amount.
- *
- * @param string $column
- * @param float|int $amount
- * @return int
- */
- protected function increment($column, $amount = 1, array $extra = [])
- {
- return $this->incrementOrDecrement($column, $amount, $extra, 'increment');
- }
- /**
- * Decrement a column's value by a given amount.
- *
- * @param string $column
- * @param float|int $amount
- * @return int
- */
- protected function decrement($column, $amount = 1, array $extra = [])
- {
- return $this->incrementOrDecrement($column, $amount, $extra, 'decrement');
- }
- /**
- * Run the increment or decrement method on the model.
- *
- * @param string $column
- * @param float|int $amount
- * @param array $extra
- * @param string $method
- * @return int
- */
- protected function incrementOrDecrement($column, $amount, $extra, $method)
- {
- $query = $this->newModelQuery();
- if (! $this->exists) {
- return $query->{$method}($column, $amount, $extra);
- }
- $this->{$column} = $this->{$column} + ($method === 'increment' ? $amount : $amount * -1);
- $this->forceFill($extra);
- $columns = array_merge(array_keys($extra), [$column]);
- return tap($this->setKeysForSaveQuery($query)->{$method}($column, $amount, $extra), function () use ($columns) {
- $this->syncChanges($columns);
- $this->syncOriginalAttributes($columns);
- });
- }
- /**
- * Perform any actions that are necessary after the model is saved.
- */
- protected function finishSave(array $options)
- {
- $this->fireModelEvent('saved');
- if ($this->isDirty() && ($options['touch'] ?? true)) {
- $this->touchOwners();
- }
- $this->syncOriginal();
- }
- /**
- * Perform a model update operation.
- *
- * @return bool
- */
- protected function performUpdate(Builder $query)
- {
- // If the updating event returns false, we will cancel the update operation so
- // developers can hook Validation systems into their models and cancel this
- // operation if the model does not pass validation. Otherwise, we update.
- if ($event = $this->fireModelEvent('updating')) {
- if ($event instanceof StoppableEventInterface && $event->isPropagationStopped()) {
- return false;
- }
- }
- // First we need to create a fresh query instance and touch the creation and
- // update timestamp on the model which are maintained by us for developer
- // convenience. Then we will just continue saving the model instances.
- if ($this->usesTimestamps()) {
- $this->updateTimestamps();
- }
- // Once we have run the update operation, we will fire the "updated" event for
- // this model instance. This will allow developers to hook into these after
- // models are updated, giving them a chance to do any special processing.
- $dirty = $this->getDirty();
- if (count($dirty) > 0) {
- $this->setKeysForSaveQuery($query)->update($dirty);
- $this->syncChanges();
- $this->fireModelEvent('updated');
- }
- return true;
- }
- /**
- * Set the keys for a save update query.
- *
- * @return Builder
- */
- protected function setKeysForSaveQuery(Builder $query)
- {
- $query->where($this->getKeyName(), '=', $this->getKeyForSaveQuery());
- return $query;
- }
- /**
- * Get the primary key value for a save query.
- */
- protected function getKeyForSaveQuery()
- {
- return $this->original[$this->getKeyName()] ?? $this->getKey();
- }
- /**
- * Perform a model insert operation.
- *
- * @return bool
- */
- protected function performInsert(Builder $query)
- {
- if ($event = $this->fireModelEvent('creating')) {
- if ($event instanceof StoppableEventInterface && $event->isPropagationStopped()) {
- return false;
- }
- }
- // First we'll need to create a fresh query instance and touch the creation and
- // update timestamps on this model, which are maintained by us for developer
- // convenience. After, we will just continue saving these model instances.
- if ($this->usesTimestamps()) {
- $this->updateTimestamps();
- }
- // If the model has an incrementing key, we can use the "insertGetId" method on
- // the query builder, which will give us back the final inserted ID for this
- // table from the database. Not all tables have to be incrementing though.
- $attributes = $this->getAttributes();
- if ($this->getIncrementing()) {
- $this->insertAndSetId($query, $attributes);
- }
- // If the table isn't incrementing we'll simply insert these attributes as they
- // are. These attribute arrays must contain an "id" column previously placed
- // there by the developer as the manually determined key for these models.
- else {
- if (empty($attributes)) {
- return true;
- }
- $query->insert($attributes);
- }
- // We will go ahead and set the exists property to true, so that it is set when
- // the created event is fired, just in case the developer tries to update it
- // during the event. This will allow them to do so and run an update here.
- $this->exists = true;
- $this->wasRecentlyCreated = true;
- $this->fireModelEvent('created');
- return true;
- }
- /**
- * Insert the given attributes and set the ID on the model.
- *
- * @param array $attributes
- */
- protected function insertAndSetId(Builder $query, $attributes)
- {
- $id = $query->insertGetId($attributes, $keyName = $this->getKeyName());
- $this->setAttribute($keyName, $id);
- }
- /**
- * Perform the actual delete query on this model instance.
- */
- protected function performDeleteOnModel()
- {
- $this->setKeysForSaveQuery($this->newModelQuery())->delete();
- $this->exists = false;
- }
- /**
- * Get a new query builder instance for the connection.
- *
- * @return QueryBuilder
- */
- protected function newBaseQueryBuilder()
- {
- $connection = $this->getConnection();
- return new QueryBuilder($connection, $connection->getQueryGrammar(), $connection->getPostProcessor());
- }
- /**
- * Initialize any initializable traits on the model.
- */
- protected function initializeTraits(): void
- {
- foreach (TraitInitializers::$container[static::class] ?? [] as $method) {
- $this->{$method}();
- }
- }
- }
|