Skip to content

Commit

Permalink
Merge pull request #46 from hagabaka/43-fix-json-column-double-encoding
Browse files Browse the repository at this point in the history
43 fix json column double encoding
  • Loading branch information
nonoesp authored Sep 1, 2020
2 parents 9abd41d + 5d0aa71 commit 5ebbaba
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Mpociot/Versionable/VersionableTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ protected function versionablePostSave()
$version->versionable_id = $this->getKey();
$version->versionable_type = get_class($this);
$version->user_id = $this->getAuthUserId();
$version->model_data = serialize($this->getAttributes());
$version->model_data = serialize($this->attributesToArray());

if (!empty( $this->reason )) {
$version->reason = $this->reason;
Expand Down
16 changes: 16 additions & 0 deletions tests/VersionableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,15 @@ public function testGetVersionModel()

}

public function testGetVersionModelWithJsonField()
{
$model = new ModelWithJsonField();
$model->json_field = ["foo" => "bar"];
$model->save();

$this->assertEquals(["foo" => "bar"], $model->getVersionModel(1)->json_field);
}

public function testUseReasonAttribute()
{
// Create 3 versions
Expand Down Expand Up @@ -547,4 +556,11 @@ class ModelWithDynamicVersion extends Model
use VersionableTrait ;
protected $versionClass = DynamicVersionModel::class ;
}
class ModelWithJsonField extends Model
{
const TABLENAME = 'table_with_json_field';
public $table = self::TABLENAME ;
use VersionableTrait ;
protected $casts = ['json_field' => 'array'];
}

6 changes: 6 additions & 0 deletions tests/VersionableTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,5 +65,11 @@ public function migrateUsersTable()
$table->index('versionable_id');
$table->timestamps();
});

$this->app['db']->connection()->getSchemaBuilder()->create(ModelWithJsonField::TABLENAME, function ($table) {
$table->increments('id');
$table->json('json_field');
$table->timestamps();
});
}
}

0 comments on commit 5ebbaba

Please sign in to comment.