From 9de73ccf5279dc4ddf00178bcb817cfc5a56e489 Mon Sep 17 00:00:00 2001 From: Max Wong Date: Mon, 5 Sep 2022 13:43:30 +0100 Subject: [PATCH 1/5] enable 'where on join query with alias' --- src/Entries/EntryQueryBuilder.php | 15 ++++++- tests/Data/Entries/EntryQueryBuilderTest.php | 42 ++++++++++++++++++++ 2 files changed, 56 insertions(+), 1 deletion(-) diff --git a/src/Entries/EntryQueryBuilder.php b/src/Entries/EntryQueryBuilder.php index 8e94f2b0..e5a428b7 100644 --- a/src/Entries/EntryQueryBuilder.php +++ b/src/Entries/EntryQueryBuilder.php @@ -18,6 +18,8 @@ class EntryQueryBuilder extends EloquentQueryBuilder implements QueryBuilder 'date', 'collection', 'created_at', 'updated_at', ]; + protected $isApplyColumnCheck = true; + protected function transform($items, $columns = []) { $items = EntryCollection::make($items)->map(function ($model) { @@ -37,10 +39,12 @@ protected function column($column) $column = 'origin_id'; } - if (! in_array($column, self::COLUMNS)) { + if ($this->isApplyColumnCheck() && ! in_array($column, self::COLUMNS)) { if (! Str::startsWith($column, 'data->')) { $column = 'data->'.$column; } + } else { + $column = parent::column($column); } return $column; @@ -76,4 +80,13 @@ public function count() return parent::count(); } + + public function setApplyColumnCheck($value = true) { + $this->isApplyColumnCheck = $value; + return $this; + } + + public function isApplyColumnCheck($value = null) { + return $this->isApplyColumnCheck; + } } diff --git a/tests/Data/Entries/EntryQueryBuilderTest.php b/tests/Data/Entries/EntryQueryBuilderTest.php index b47c2f37..efbd7c83 100644 --- a/tests/Data/Entries/EntryQueryBuilderTest.php +++ b/tests/Data/Entries/EntryQueryBuilderTest.php @@ -688,4 +688,46 @@ public function entries_are_found_using_offset() $this->assertCount(2, $entries); $this->assertEquals(['Post 2', 'Post 3'], $entries->map->title->all()); } + + /** @test */ + public function entries_can_be_retrieved_on_join_table_conditions() + { + Collection::make('posts')->save(); + EntryFactory::id('1')->slug('post-1')->collection('posts')->data(['title' => 'Post 1', 'author' => 'John Doe', 'location' => 4])->create(); + EntryFactory::id('2')->slug('post-2')->collection('posts')->data(['title' => 'Post 2', 'author' => 'John Doe'])->create(); + EntryFactory::id('3')->slug('post-3')->collection('posts')->data(['title' => 'Post 3', 'author' => 'John Doe', 'location' => 4])->create(); + Collection::make('locations')->save(); + + $locations = [ + 4 => ['slug' => 'shaldon', 'title' => 'Shaldon'], + 5 => ['slug' => 'cambridge', 'title' => 'Cambridge'], + 6 => ['slug' => 'london', 'title' => 'London'], + ]; + + foreach (range(4, 6) as $index) { + + + EntryFactory::id($index)->slug($locations[$index]['slug'])->collection('locations') + ->data(['title' => $locations[$index]['title']])->create(); + } + + $query = Entry::query() + ->setApplyColumnCheck(false) + ->join('entries as e', fn ($join) => $join + ->whereColumn('e.id', 'entries.id') + ->where('e.collection', 'posts') + )->leftJoin('entries as locations', function ($join) { + $join + ->where('locations.collection', 'locations') + ->on('locations.id', 'e.data->location'); + }) + ->where('e.data->title', 'like', '%post%') + ->where('locations.slug', 'shaldon') + ; + + $entries = $query->get(); + + // successfully retrieved 2 results + $this->assertCount(2, $entries); + } } From 725a9991a7ea6a0f209e9b1eeebd1241cb4dbe38 Mon Sep 17 00:00:00 2001 From: Max Wong Date: Mon, 5 Sep 2022 13:47:54 +0100 Subject: [PATCH 2/5] Apply StyleCI --- src/Entries/EntryQueryBuilder.php | 7 +++++-- tests/Data/Entries/EntryQueryBuilderTest.php | 5 +---- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/Entries/EntryQueryBuilder.php b/src/Entries/EntryQueryBuilder.php index e5a428b7..3f212f48 100644 --- a/src/Entries/EntryQueryBuilder.php +++ b/src/Entries/EntryQueryBuilder.php @@ -81,12 +81,15 @@ public function count() return parent::count(); } - public function setApplyColumnCheck($value = true) { + public function setApplyColumnCheck($value = true) + { $this->isApplyColumnCheck = $value; + return $this; } - public function isApplyColumnCheck($value = null) { + public function isApplyColumnCheck($value = null) + { return $this->isApplyColumnCheck; } } diff --git a/tests/Data/Entries/EntryQueryBuilderTest.php b/tests/Data/Entries/EntryQueryBuilderTest.php index efbd7c83..55da8b50 100644 --- a/tests/Data/Entries/EntryQueryBuilderTest.php +++ b/tests/Data/Entries/EntryQueryBuilderTest.php @@ -705,8 +705,6 @@ public function entries_can_be_retrieved_on_join_table_conditions() ]; foreach (range(4, 6) as $index) { - - EntryFactory::id($index)->slug($locations[$index]['slug'])->collection('locations') ->data(['title' => $locations[$index]['title']])->create(); } @@ -722,8 +720,7 @@ public function entries_can_be_retrieved_on_join_table_conditions() ->on('locations.id', 'e.data->location'); }) ->where('e.data->title', 'like', '%post%') - ->where('locations.slug', 'shaldon') - ; + ->where('locations.slug', 'shaldon'); $entries = $query->get(); From 5a08c2576670ab211dc9c2bab41a47a996f57ebc Mon Sep 17 00:00:00 2001 From: Max Wong Date: Mon, 5 Sep 2022 15:08:21 +0100 Subject: [PATCH 3/5] fine tuning for column extraction --- src/Entries/EntryQueryBuilder.php | 25 +++++--------------- tests/Data/Entries/EntryQueryBuilderTest.php | 4 ++-- 2 files changed, 8 insertions(+), 21 deletions(-) diff --git a/src/Entries/EntryQueryBuilder.php b/src/Entries/EntryQueryBuilder.php index 3f212f48..d0d9fdad 100644 --- a/src/Entries/EntryQueryBuilder.php +++ b/src/Entries/EntryQueryBuilder.php @@ -18,8 +18,6 @@ class EntryQueryBuilder extends EloquentQueryBuilder implements QueryBuilder 'date', 'collection', 'created_at', 'updated_at', ]; - protected $isApplyColumnCheck = true; - protected function transform($items, $columns = []) { $items = EntryCollection::make($items)->map(function ($model) { @@ -34,20 +32,21 @@ protected function column($column) if (! is_string($column)) { return $column; } + + $table = Str::contains($column, '.') ? Str::before($column, '.') : ''; + $column = Str::after($column, '.'); if ($column == 'origin') { $column = 'origin_id'; } - if ($this->isApplyColumnCheck() && ! in_array($column, self::COLUMNS)) { - if (! Str::startsWith($column, 'data->')) { + if (! in_array($column, self::COLUMNS)) { + if (! Str::contains($column, 'data->')) { $column = 'data->'.$column; } - } else { - $column = parent::column($column); } - return $column; + return ($table ? $table.'.' : '').$column; } public function find($id, $columns = ['*']) @@ -80,16 +79,4 @@ public function count() return parent::count(); } - - public function setApplyColumnCheck($value = true) - { - $this->isApplyColumnCheck = $value; - - return $this; - } - - public function isApplyColumnCheck($value = null) - { - return $this->isApplyColumnCheck; - } } diff --git a/tests/Data/Entries/EntryQueryBuilderTest.php b/tests/Data/Entries/EntryQueryBuilderTest.php index 55da8b50..1d3b7894 100644 --- a/tests/Data/Entries/EntryQueryBuilderTest.php +++ b/tests/Data/Entries/EntryQueryBuilderTest.php @@ -710,7 +710,6 @@ public function entries_can_be_retrieved_on_join_table_conditions() } $query = Entry::query() - ->setApplyColumnCheck(false) ->join('entries as e', fn ($join) => $join ->whereColumn('e.id', 'entries.id') ->where('e.collection', 'posts') @@ -720,7 +719,8 @@ public function entries_can_be_retrieved_on_join_table_conditions() ->on('locations.id', 'e.data->location'); }) ->where('e.data->title', 'like', '%post%') - ->where('locations.slug', 'shaldon'); + ->where('locations.slug', 'shaldon') + ; $entries = $query->get(); From d7b6dcf93b054c91149e829aa6e013ab321faa36 Mon Sep 17 00:00:00 2001 From: Max Wong Date: Mon, 5 Sep 2022 15:10:46 +0100 Subject: [PATCH 4/5] Apply Style CI --- src/Entries/EntryQueryBuilder.php | 2 +- tests/Data/Entries/EntryQueryBuilderTest.php | 3 +-- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/src/Entries/EntryQueryBuilder.php b/src/Entries/EntryQueryBuilder.php index d0d9fdad..10078faf 100644 --- a/src/Entries/EntryQueryBuilder.php +++ b/src/Entries/EntryQueryBuilder.php @@ -32,7 +32,7 @@ protected function column($column) if (! is_string($column)) { return $column; } - + $table = Str::contains($column, '.') ? Str::before($column, '.') : ''; $column = Str::after($column, '.'); diff --git a/tests/Data/Entries/EntryQueryBuilderTest.php b/tests/Data/Entries/EntryQueryBuilderTest.php index 1d3b7894..1116fa56 100644 --- a/tests/Data/Entries/EntryQueryBuilderTest.php +++ b/tests/Data/Entries/EntryQueryBuilderTest.php @@ -719,8 +719,7 @@ public function entries_can_be_retrieved_on_join_table_conditions() ->on('locations.id', 'e.data->location'); }) ->where('e.data->title', 'like', '%post%') - ->where('locations.slug', 'shaldon') - ; + ->where('locations.slug', 'shaldon'); $entries = $query->get(); From 6c598af352024457b3b94c28336d64e57667e454 Mon Sep 17 00:00:00 2001 From: Jason Varga Date: Fri, 11 Nov 2022 16:41:24 -0500 Subject: [PATCH 5/5] revert --- src/Entries/EntryQueryBuilder.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Entries/EntryQueryBuilder.php b/src/Entries/EntryQueryBuilder.php index 10078faf..67b909f6 100644 --- a/src/Entries/EntryQueryBuilder.php +++ b/src/Entries/EntryQueryBuilder.php @@ -41,7 +41,7 @@ protected function column($column) } if (! in_array($column, self::COLUMNS)) { - if (! Str::contains($column, 'data->')) { + if (! Str::startsWith($column, 'data->')) { $column = 'data->'.$column; } }