Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 21 additions & 23 deletions src/Concerns/HasSnapshot.php
Original file line number Diff line number Diff line change
Expand Up @@ -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}();
Expand All @@ -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');
// });
}

/**
Expand Down Expand Up @@ -186,7 +179,7 @@ protected function getSnapshotSource()
return $class::find($sourceModelOwnerId);
}

return $this->source;
return $this->snapshotSource;
}

/**
Expand All @@ -197,13 +190,18 @@ protected function getSnapshotSource()
*/
public function prepareDataToSnapshot(): array
{
$source = $this->snapshotSource()->with($this->getSourceRelationshipsToSnapshot())->first();
$sourceRelationships = $this->getSourceRelationshipsToSnapshot();
$source = $this->snapshotSource()->with($sourceRelationships)->first();

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);
}
Expand All @@ -216,7 +214,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)) {
Expand Down Expand Up @@ -349,7 +347,7 @@ public function saveSnapshot()
return;
}

$this->snapshot()->updateOrCreate(
$snapshot = $this->snapshot()->updateOrCreate(
[
'snapshotable_id' => $this->id,
'snapshotable_type' => get_class($this),
Expand All @@ -359,7 +357,7 @@ public function saveSnapshot()
[]
);

$this->snapshot()->update([
$snapshot->update([
'data' => $this->prepareDataToSnapshot()
]);
}
Expand All @@ -368,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;

Expand Down Expand Up @@ -433,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){
Expand Down Expand Up @@ -518,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}();

Expand Down
5 changes: 3 additions & 2 deletions src/Concerns/Relationships.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -32,7 +33,7 @@ public function source(): \Illuminate\Database\Eloquent\Relations\HasOneThrough
secondKey: 'id',
localKey: 'id',
secondLocalKey: 'source_id',
);
)->where('source_type', static::getSnapshotSourceClass());
}

/**
Expand All @@ -49,6 +50,6 @@ public function snapshotSource(): \Illuminate\Database\Eloquent\Relations\HasOne
secondKey: 'id',
localKey: 'id',
secondLocalKey: 'source_id',
);
)->where('source_type', static::getSnapshotSourceClass());
}
}