From beca1eead160a3ca4b37a9f3b7dc45a236e14ecb Mon Sep 17 00:00:00 2001 From: OoBook Date: Tue, 8 Jul 2025 02:24:36 +0300 Subject: [PATCH 1/3] fix(trait): streamline data preparation and snapshot saving in HasSnapshot for improved clarity and performance - Used getSnapshotSource method instead of source relationship query. --- src/Concerns/HasSnapshot.php | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/src/Concerns/HasSnapshot.php b/src/Concerns/HasSnapshot.php index 92f5241..0f04b78 100644 --- a/src/Concerns/HasSnapshot.php +++ b/src/Concerns/HasSnapshot.php @@ -197,13 +197,18 @@ protected function getSnapshotSource() */ public function prepareDataToSnapshot(): array { - $source = $this->snapshotSource()->with($this->getSourceRelationshipsToSnapshot())->first(); + $sourceRelationships = $this->getSourceRelationshipsToSnapshot(); + $source = $this->getSnapshotSource(); + + foreach($sourceRelationships as $relationship){ + $source->load($relationship); + } // Instead of using toArray(), we'll build the array manually to preserve relationship names $data = $source->attributesToArray(); // Add relationships with original names - foreach ($this->getSourceRelationshipsToSnapshot() as $relation) { + foreach ($sourceRelationships as $relation) { if ($source->relationLoaded($relation)) { $data[$relation] = $source->getRelation($relation); } @@ -216,7 +221,7 @@ public function prepareDataToSnapshot(): array $data[$field] = $value; } - foreach ($this->getSourceRelationshipsToSnapshot() as $relationshipName) { + foreach ($sourceRelationships as $relationshipName) { $relatedClass = $source->{$relationshipName}()->getRelated(); if ($this::class === get_class($relatedClass)) { @@ -349,7 +354,7 @@ public function saveSnapshot() return; } - $this->snapshot()->updateOrCreate( + $snapshot = $this->snapshot()->updateOrCreate( [ 'snapshotable_id' => $this->id, 'snapshotable_type' => get_class($this), @@ -359,7 +364,7 @@ public function saveSnapshot() [] ); - $this->snapshot()->update([ + $snapshot->update([ 'data' => $this->prepareDataToSnapshot() ]); } From f5a31e807a86f171471eb278b7ce7c1fdd0614a4 Mon Sep 17 00:00:00 2001 From: OoBook Date: Wed, 9 Jul 2025 17:36:26 +0300 Subject: [PATCH 2/3] refactor(relationships): deprecate source method in favor of snapshotSource and streamline query conditions --- src/Concerns/Relationships.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/Concerns/Relationships.php b/src/Concerns/Relationships.php index 8332c12..d5f0b23 100644 --- a/src/Concerns/Relationships.php +++ b/src/Concerns/Relationships.php @@ -22,6 +22,7 @@ public function snapshot(): \Illuminate\Database\Eloquent\Relations\MorphOne * Defines the relationship with the source model. * * @return \Illuminate\Database\Eloquent\Relations\HasOneThrough + * @deprecated Use snapshotSource() instead */ public function source(): \Illuminate\Database\Eloquent\Relations\HasOneThrough { @@ -32,7 +33,7 @@ public function source(): \Illuminate\Database\Eloquent\Relations\HasOneThrough secondKey: 'id', localKey: 'id', secondLocalKey: 'source_id', - ); + )->where('source_type', static::getSnapshotSourceClass()); } /** @@ -49,6 +50,6 @@ public function snapshotSource(): \Illuminate\Database\Eloquent\Relations\HasOne secondKey: 'id', localKey: 'id', secondLocalKey: 'source_id', - ); + )->where('source_type', static::getSnapshotSourceClass()); } } \ No newline at end of file From c41771d8b36c95bac7a2dd855875b647e407b0e5 Mon Sep 17 00:00:00 2001 From: OoBook Date: Wed, 9 Jul 2025 17:36:49 +0300 Subject: [PATCH 3/3] refactor(relationships): replace 'source' references with 'snapshotSource' for consistency and clarity in HasSnapshot --- src/Concerns/HasSnapshot.php | 31 ++++++++++++------------------- 1 file changed, 12 insertions(+), 19 deletions(-) diff --git a/src/Concerns/HasSnapshot.php b/src/Concerns/HasSnapshot.php index 0f04b78..319e7dd 100644 --- a/src/Concerns/HasSnapshot.php +++ b/src/Concerns/HasSnapshot.php @@ -110,7 +110,7 @@ public static function bootHasSnapshot() foreach ($syncedSourceRelationships as $relationship) { if(!method_exists(static::class, $relationship)){ static::resolveRelationUsing($relationship, function ($snapshotable) use ($sourceInstance, $relationship) { - $source = $snapshotable->source; + $source = $snapshotable->snapshotSource; if($source){ return $source->{$relationship}(); @@ -120,13 +120,6 @@ public static function bootHasSnapshot() }); } } - - // if(method_exists($sourceInstance, 'definedRelationsTypes')){ - // } - - // static::resolveRelationUsing('source', function ($model) { - // return $model->hasOneThrough(Snapshot::class, Snapshot::class, 'snapshotable_id', 'id', 'id', 'source_id'); - // }); } /** @@ -186,7 +179,7 @@ protected function getSnapshotSource() return $class::find($sourceModelOwnerId); } - return $this->source; + return $this->snapshotSource; } /** @@ -198,7 +191,7 @@ protected function getSnapshotSource() public function prepareDataToSnapshot(): array { $sourceRelationships = $this->getSourceRelationshipsToSnapshot(); - $source = $this->getSnapshotSource(); + $source = $this->snapshotSource()->with($sourceRelationships)->first(); foreach($sourceRelationships as $relationship){ $source->load($relationship); @@ -373,9 +366,9 @@ public function attributesToArray(): array { $attributes = parent::attributesToArray(); - $source = $this->relationLoaded('source') - ? $this->getRelation('source') - : $this->source; + $source = $this->relationLoaded('snapshotSource') + ? $this->getRelation('snapshotSource') + : $this->snapshotSource; $snapshot = $this->snapshot; @@ -438,9 +431,9 @@ public function __get($key) return $snapshot->source_id; } - $source = $this->relationLoaded('source') - ? $this->getRelation('source') - : $this->source; + $source = $this->relationLoaded('snapshotSource') + ? $this->getRelation('snapshotSource') + : $this->snapshotSource; if($this->fieldIsSnapshotSynced($key)){ if($source){ @@ -523,9 +516,9 @@ public function __call($method, $parameters) if($methodReflector->hasReturnType() && preg_match("{$relationClassesPattern}", $methodReflector->getReturnType() )){ - $source = $this->relationLoaded('source') - ? $this->getRelation('source') - : $this->source; + $source = $this->relationLoaded('snapshotSource') + ? $this->getRelation('snapshotSource') + : $this->snapshotSource; $query = $source->{$method}();