mirror of
https://github.com/archtechx/virtualcolumn.git
synced 2025-12-13 13:24:03 +00:00
add virtual columns array
This commit is contained in:
parent
ab3f943990
commit
6ca7c419c2
3 changed files with 101 additions and 1 deletions
|
|
@ -45,7 +45,10 @@ trait VirtualColumn
|
|||
}
|
||||
|
||||
foreach ($model->getAttributes() as $key => $value) {
|
||||
if (! in_array($key, static::getCustomColumns())) {
|
||||
|
||||
if ((static::getVirtualColumns() && in_array($key, static::getVirtualColumns(), true))
|
||||
|| ! in_array($key, static::getCustomColumns())
|
||||
) {
|
||||
$current = $model->getAttribute(static::getDataColumn()) ?? [];
|
||||
|
||||
$model->setAttribute(static::getDataColumn(), array_merge($current, [
|
||||
|
|
@ -133,6 +136,13 @@ trait VirtualColumn
|
|||
return 'data';
|
||||
}
|
||||
|
||||
public static function getVirtualColumns(): array
|
||||
{
|
||||
return [
|
||||
//
|
||||
];
|
||||
}
|
||||
|
||||
public static function getCustomColumns(): array
|
||||
{
|
||||
return [
|
||||
|
|
|
|||
|
|
@ -57,6 +57,49 @@ class VirtualColumnTest extends TestCase
|
|||
$this->assertSame(null, $model->data);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function keys_defined_in_the_virtual_columns_function_go_into_data_json_column()
|
||||
{
|
||||
$model = VirtualModel::create([
|
||||
'id' => 1,
|
||||
'foo' => 'bar',
|
||||
]);
|
||||
|
||||
// Test that model works correctly
|
||||
$this->assertSame(1, $model->id);
|
||||
$this->assertSame('bar', $model->foo);
|
||||
$this->assertSame(null, $model->data);
|
||||
|
||||
// Low level test to assert database structure
|
||||
$this->assertSame(['foo' => 'bar'], json_decode(DB::table('virtual_models')->where('id', $model->id)->first()->data, true));
|
||||
$this->assertSame(null, DB::table('virtual_models')->where('id', $model->id)->first()->foo ?? null);
|
||||
|
||||
// Model has the correct structure when retrieved
|
||||
$model = VirtualModel::first();
|
||||
$this->assertSame('bar', $model->foo);
|
||||
$this->assertSame('bar', $model->getOriginal('foo'));
|
||||
$this->assertSame(null, $model->data);
|
||||
|
||||
// Model can be updated
|
||||
$model->update([
|
||||
'foo' => 'baz',
|
||||
'abc' => 'xyz',
|
||||
]);
|
||||
|
||||
$this->assertSame('baz', $model->foo);
|
||||
$this->assertSame('baz', $model->getOriginal('foo'));
|
||||
$this->assertSame('xyz', $model->abc);
|
||||
$this->assertSame('xyz', $model->getOriginal('abc'));
|
||||
$this->assertSame(null, $model->data);
|
||||
|
||||
// Model can be retrieved after update & is structure correctly
|
||||
$model = VirtualModel::first();
|
||||
|
||||
$this->assertSame('baz', $model->foo);
|
||||
$this->assertSame('xyz', $model->abc);
|
||||
$this->assertSame(null, $model->data);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function model_is_always_decoded_when_accessed_by_user_event()
|
||||
{
|
||||
|
|
@ -125,6 +168,24 @@ class MyModel extends Model
|
|||
}
|
||||
}
|
||||
|
||||
class VirtualModel extends Model
|
||||
{
|
||||
use VirtualColumn;
|
||||
|
||||
protected $guarded = [];
|
||||
public $timestamps = false;
|
||||
|
||||
public static function getVirtualColumns(): array
|
||||
{
|
||||
return [
|
||||
'foo',
|
||||
'abc',
|
||||
'baz',
|
||||
'xyz',
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
class FooModel extends Model
|
||||
{
|
||||
use VirtualColumn;
|
||||
|
|
|
|||
|
|
@ -0,0 +1,29 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
class CreateVirtualModelsTable extends Migration
|
||||
{
|
||||
/**
|
||||
* Run the migrations.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function up()
|
||||
{
|
||||
Schema::create('virtual_models', function (Blueprint $table) {
|
||||
$table->increments('id');
|
||||
|
||||
$table->json('data');
|
||||
});
|
||||
}
|
||||
|
||||
public function down()
|
||||
{
|
||||
Schema::dropIfExists('foo_models');
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue