From 069321a24f30ac2abbdfbcdf53f57d9bad2a61c4 Mon Sep 17 00:00:00 2001 From: Charles Sterling Date: Mon, 7 Jun 2021 08:59:47 -0700 Subject: [PATCH] Get actual class name from morph map --- src/Mpociot/Versionable/Version.php | 3 ++- tests/VersionableTest.php | 20 ++++++++++++++++++-- 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/src/Mpociot/Versionable/Version.php b/src/Mpociot/Versionable/Version.php index d145eb1..add5cba 100644 --- a/src/Mpociot/Versionable/Version.php +++ b/src/Mpociot/Versionable/Version.php @@ -51,7 +51,8 @@ public function getModel() ? stream_get_contents($this->model_data,-1,0) : $this->model_data; - $model = new $this->versionable_type(); + $className = self::getActualClassNameForMorph($this->versionable_type); + $model = new $className(); $model->unguard(); $model->fill(unserialize($modelData)); $model->exists = true; diff --git a/tests/VersionableTest.php b/tests/VersionableTest.php index 286f26f..e146f33 100644 --- a/tests/VersionableTest.php +++ b/tests/VersionableTest.php @@ -1,12 +1,13 @@ assertArrayNotHasKey('password', $user->toArray()); } + + public function testWhereModelHasMorphMap() + { + Relation::morphMap(['users' => TestVersionableUser::class]); + $user = new TestVersionableUser(); + $user->name = "Test"; + $user->email = "example@test.php"; + $user->password = "12345"; + $user->last_login = $user->freshTimestamp(); + $user->save(); + + $version = $user->currentVersion(); + $this->assertEquals( $user->attributesToArray(), $version->getModel()->attributesToArray() ); + Relation::morphMap([], false); + } }