From 43f0b2c30fad9b6c245ec8856c49f252fafa290f Mon Sep 17 00:00:00 2001 From: lukinovec Date: Fri, 21 Oct 2022 11:32:12 +0200 Subject: [PATCH 1/5] Add method for generating column names --- src/VirtualColumn.php | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/VirtualColumn.php b/src/VirtualColumn.php index f7a6e3c..7a177e0 100644 --- a/src/VirtualColumn.php +++ b/src/VirtualColumn.php @@ -139,4 +139,13 @@ trait VirtualColumn 'id', ]; } + + public function generateColumnName(string $column): string + { + if (in_array($column, static::getCustomColumns())) { + return $column; + } + + return static::getDataColumn() . '->' . $column; + } } From c0cd839acc51d1c9b78ee5f9c38d0ef8d9d5b67b Mon Sep 17 00:00:00 2001 From: lukinovec Date: Fri, 21 Oct 2022 11:49:10 +0200 Subject: [PATCH 2/5] Add test for generating the column name --- tests/VirtualColumnTest.php | 39 +++++++++++++++++++ ...2_10_21_000001_create_foo_models_table.php | 32 +++++++++++++++ 2 files changed, 71 insertions(+) create mode 100644 tests/etc/migrations/2022_10_21_000001_create_foo_models_table.php diff --git a/tests/VirtualColumnTest.php b/tests/VirtualColumnTest.php index 812b599..ab45274 100644 --- a/tests/VirtualColumnTest.php +++ b/tests/VirtualColumnTest.php @@ -88,6 +88,23 @@ class VirtualColumnTest extends TestCase MyModel::first(); } + /** @test */ + public function column_names_are_generated_correctly() + { + // AnotherModel's virtual data column name is 'virtual' + $virtualColumnName = 'virtual->foo'; + $customColumnName = 'custom1'; + + /** @var FooModel $model */ + $model = FooModel::create([ + 'custom1' => $customColumnName, + 'foo' => $virtualColumnName + ]); + + $this->assertSame($customColumnName, $model->generateColumnName('custom1')); + $this->assertSame($virtualColumnName, $model->generateColumnName('foo')); + } + // maybe add an explicit test that the saving() and updating() listeners don't run twice? } @@ -107,3 +124,25 @@ class MyModel extends Model ]; } } + +class FooModel extends Model +{ + use VirtualColumn; + + protected $guarded = []; + public $timestamps = false; + + public static function getCustomColumns(): array + { + return [ + 'id', + 'custom1', + 'custom2', + ]; + } + + public static function getDataColumn(): string + { + return 'virtual'; + } +} diff --git a/tests/etc/migrations/2022_10_21_000001_create_foo_models_table.php b/tests/etc/migrations/2022_10_21_000001_create_foo_models_table.php new file mode 100644 index 0000000..98aa537 --- /dev/null +++ b/tests/etc/migrations/2022_10_21_000001_create_foo_models_table.php @@ -0,0 +1,32 @@ +increments('id'); + + $table->string('custom1')->nullable(); + $table->string('custom2')->nullable(); + + $table->json('virtual'); + }); + } + + public function down() + { + Schema::dropIfExists('foo_models'); + } +} From 5881f13b815eb7650ec83dfde8d4bf7270fb82b3 Mon Sep 17 00:00:00 2001 From: lukinovec Date: Fri, 21 Oct 2022 11:51:05 +0200 Subject: [PATCH 3/5] Correct comment --- tests/VirtualColumnTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/VirtualColumnTest.php b/tests/VirtualColumnTest.php index ab45274..88a198b 100644 --- a/tests/VirtualColumnTest.php +++ b/tests/VirtualColumnTest.php @@ -91,7 +91,7 @@ class VirtualColumnTest extends TestCase /** @test */ public function column_names_are_generated_correctly() { - // AnotherModel's virtual data column name is 'virtual' + // FooModel's virtual data column name is 'virtual' $virtualColumnName = 'virtual->foo'; $customColumnName = 'custom1'; From 73d7918170c8f68275f095e85b2c800a67cc94a6 Mon Sep 17 00:00:00 2001 From: lukinovec Date: Fri, 21 Oct 2022 13:56:03 +0200 Subject: [PATCH 4/5] Rename generateColumnName --- src/VirtualColumn.php | 2 +- tests/VirtualColumnTest.php | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/VirtualColumn.php b/src/VirtualColumn.php index 7a177e0..1943a80 100644 --- a/src/VirtualColumn.php +++ b/src/VirtualColumn.php @@ -140,7 +140,7 @@ trait VirtualColumn ]; } - public function generateColumnName(string $column): string + public function getColumnForQuery(string $column): string { if (in_array($column, static::getCustomColumns())) { return $column; diff --git a/tests/VirtualColumnTest.php b/tests/VirtualColumnTest.php index 88a198b..247b189 100644 --- a/tests/VirtualColumnTest.php +++ b/tests/VirtualColumnTest.php @@ -101,8 +101,8 @@ class VirtualColumnTest extends TestCase 'foo' => $virtualColumnName ]); - $this->assertSame($customColumnName, $model->generateColumnName('custom1')); - $this->assertSame($virtualColumnName, $model->generateColumnName('foo')); + $this->assertSame($customColumnName, $model->getColumnForQuery('custom1')); + $this->assertSame($virtualColumnName, $model->getColumnForQuery('foo')); } // maybe add an explicit test that the saving() and updating() listeners don't run twice? From ac0dccc6e7c98041a2bf632e7f240b8fc8405f21 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Samuel=20=C5=A0tancl?= Date: Fri, 21 Oct 2022 14:08:35 +0200 Subject: [PATCH 5/5] add docblock --- src/VirtualColumn.php | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/VirtualColumn.php b/src/VirtualColumn.php index 1943a80..82de5aa 100644 --- a/src/VirtualColumn.php +++ b/src/VirtualColumn.php @@ -140,9 +140,14 @@ trait VirtualColumn ]; } + /** + * Get a column name for an attribute that can be used in SQL queries. + * + * (`foo` or `data->foo` depending on whether `foo` is in custom columns) + */ public function getColumnForQuery(string $column): string { - if (in_array($column, static::getCustomColumns())) { + if (in_array($column, static::getCustomColumns(), true)) { return $column; }