Skip to content

Commit 4c6be03

Browse files
maxwkfjasonvarga
andauthored
Fix table name usage in clauses (#56)
Co-authored-by: Jason Varga <[email protected]>
1 parent 7fdc919 commit 4c6be03

File tree

2 files changed

+42
-1
lines changed

2 files changed

+42
-1
lines changed

src/Entries/EntryQueryBuilder.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ protected function column($column)
3434
return $column;
3535
}
3636

37+
$table = Str::contains($column, '.') ? Str::before($column, '.') : '';
38+
$column = Str::after($column, '.');
39+
3740
if ($column == 'origin') {
3841
$column = 'origin_id';
3942
}
@@ -44,7 +47,7 @@ protected function column($column)
4447
}
4548
}
4649

47-
return $column;
50+
return ($table ? $table.'.' : '').$column;
4851
}
4952

5053
public function find($id, $columns = ['*'])

tests/Data/Entries/EntryQueryBuilderTest.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -688,4 +688,42 @@ public function entries_are_found_using_offset()
688688
$this->assertCount(2, $entries);
689689
$this->assertEquals(['Post 2', 'Post 3'], $entries->map->title->all());
690690
}
691+
692+
/** @test */
693+
public function entries_can_be_retrieved_on_join_table_conditions()
694+
{
695+
Collection::make('posts')->save();
696+
EntryFactory::id('1')->slug('post-1')->collection('posts')->data(['title' => 'Post 1', 'author' => 'John Doe', 'location' => 4])->create();
697+
EntryFactory::id('2')->slug('post-2')->collection('posts')->data(['title' => 'Post 2', 'author' => 'John Doe'])->create();
698+
EntryFactory::id('3')->slug('post-3')->collection('posts')->data(['title' => 'Post 3', 'author' => 'John Doe', 'location' => 4])->create();
699+
Collection::make('locations')->save();
700+
701+
$locations = [
702+
4 => ['slug' => 'shaldon', 'title' => 'Shaldon'],
703+
5 => ['slug' => 'cambridge', 'title' => 'Cambridge'],
704+
6 => ['slug' => 'london', 'title' => 'London'],
705+
];
706+
707+
foreach (range(4, 6) as $index) {
708+
EntryFactory::id($index)->slug($locations[$index]['slug'])->collection('locations')
709+
->data(['title' => $locations[$index]['title']])->create();
710+
}
711+
712+
$query = Entry::query()
713+
->join('entries as e', fn ($join) => $join
714+
->whereColumn('e.id', 'entries.id')
715+
->where('e.collection', 'posts')
716+
)->leftJoin('entries as locations', function ($join) {
717+
$join
718+
->where('locations.collection', 'locations')
719+
->on('locations.id', 'e.data->location');
720+
})
721+
->where('e.data->title', 'like', '%post%')
722+
->where('locations.slug', 'shaldon');
723+
724+
$entries = $query->get();
725+
726+
// successfully retrieved 2 results
727+
$this->assertCount(2, $entries);
728+
}
691729
}

0 commit comments

Comments
 (0)