From cc9a54d92113635ac898d7d9999f4f583f701894 Mon Sep 17 00:00:00 2001 From: Ollie Read Date: Thu, 18 Nov 2021 12:41:06 +0000 Subject: [PATCH] fix: Fix bug where the wrong class was important for the relationship --- src/Concerns/MapsIdentity.php | 171 +++++++++++++++++----------------- 1 file changed, 87 insertions(+), 84 deletions(-) diff --git a/src/Concerns/MapsIdentity.php b/src/Concerns/MapsIdentity.php index 2f8bb8d..e3bddce 100644 --- a/src/Concerns/MapsIdentity.php +++ b/src/Concerns/MapsIdentity.php @@ -1,12 +1,13 @@ - Identity::storeIdentity($model->getModelIdentity(), $model)); } - /** - * Override the default newFromBuilder method to use the identity map. - * - * @see \Illuminate\Database\Eloquent\Model::newFromBuilder() - * - * @param array $attributes - * @param null $connection - * - * @return \Illuminate\Database\Eloquent\Model - * @noinspection PhpIncompatibleReturnTypeInspection - */ - public function newFromBuilder($attributes = [], $connection = null): Model - { - $attributes = (array) $attributes; - $key = $attributes[$this->getKeyName()] ?? null; - $identity = $model = null; - - if ($key !== null) { - $identity = $this->getModelIdentity($key, $connection); - - if (Identity::hasIdentity($identity)) { - $model = Identity::getIdentity($identity); - /** @noinspection NullPointerExceptionInspection */ - $this->updateModelAttributes($model, $attributes); - - return $model; - } - } - - $model = parent::newFromBuilder($attributes, $connection); - - if ($identity !== null) { - Identity::storeIdentity($model->getModelIdentity(), $model); - } - - return $model; - } - - /** - * Get the model identity. - * - * @param null $id - * @param string|null $connection - * - * @return \OllieCodes\Eloquent\Identity\ModelIdentity - */ - public function getModelIdentity($id = null, ?string $connection = null): ModelIdentity - { - $connection = $connection ?? $this->getConnectionName() ?? static::getConnectionResolver()->getDefaultConnection(); - - return new ModelIdentity(static::class, $id ?? $this->getKey(), $connection); - } - - /** - * Create a new Eloquent query builder for the model. - * - * @param \Illuminate\Database\Query\Builder $query - * - * @return \Illuminate\Database\Eloquent\Builder|\OllieCodes\Eloquent\Identity\Query\Builder|static - */ - public function newEloquentBuilder($query) - { - return new Builder($query); - } - - /** - * Change the original attributes to match the new attributes, and re-add the dirty records. - * - * @param \Illuminate\Database\Eloquent\Model $model - * @param array $attributes - */ - protected function updateModelAttributes(Model $model, array $attributes = []): void - { - if (! $this->areAttributesMoreRecent($model, $attributes)) { - return; - } - - $dirtyAttributes = $model->getDirty(); - $model->setRawAttributes($attributes, true); - $model->setRawAttributes(array_merge($model->getAttributes(), $dirtyAttributes), false); - } - /** * Check if the provided attributes are newer. * @@ -145,6 +66,22 @@ protected function areAttributesMoreRecent(Model $model, array $attributes): boo return true; } + /** + * Get the model identity. + * + * @param null $id + * @param string|null $connection + * + * @return \OllieCodes\Eloquent\Identity\ModelIdentity + */ + public function getModelIdentity($id = null, ?string $connection = null): ModelIdentity + { + $connection = + $connection ?? $this->getConnectionName() ?? static::getConnectionResolver()->getDefaultConnection(); + + return new ModelIdentity(static::class, $id ?? $this->getKey(), $connection); + } + /** * Get a relationship value from a method. * @@ -195,4 +132,70 @@ protected function getRelationshipResults(Relation $relation) return $relation->getResults(); } + + /** + * Create a new Eloquent query builder for the model. + * + * @param \Illuminate\Database\Query\Builder $query + * + * @return \Illuminate\Database\Eloquent\Builder|\OllieCodes\Eloquent\Identity\Query\Builder|static + */ + public function newEloquentBuilder($query): Builder + { + return new Builder($query); + } + + /** + * Override the default newFromBuilder method to use the identity map. + * + * @param array $attributes + * @param null $connection + * + * @return \Illuminate\Database\Eloquent\Model + * @see \Illuminate\Database\Eloquent\Model::newFromBuilder() + * + */ + public function newFromBuilder($attributes = [], $connection = null): Model + { + $attributes = (array)$attributes; + $key = $attributes[$this->getKeyName()] ?? null; + $identity = $model = null; + + if ($key !== null) { + $identity = $this->getModelIdentity($key, $connection); + + if (Identity::hasIdentity($identity)) { + $model = Identity::getIdentity($identity); + /** @noinspection NullPointerExceptionInspection */ + $this->updateModelAttributes($model, $attributes); + + return $model; + } + } + + $model = parent::newFromBuilder($attributes, $connection); + + if ($identity !== null) { + Identity::storeIdentity($model->getModelIdentity(), $model); + } + + return $model; + } + + /** + * Change the original attributes to match the new attributes, and re-add the dirty records. + * + * @param \Illuminate\Database\Eloquent\Model $model + * @param array $attributes + */ + protected function updateModelAttributes(Model $model, array $attributes = []): void + { + if (! $this->areAttributesMoreRecent($model, $attributes)) { + return; + } + + $dirtyAttributes = $model->getDirty(); + $model->setRawAttributes($attributes, true); + $model->setRawAttributes(array_merge($model->getAttributes(), $dirtyAttributes), false); + } } \ No newline at end of file