From 9d302c766f39590c67aa39e99aa496ccec6b0839 Mon Sep 17 00:00:00 2001 From: mpyw Date: Wed, 6 Nov 2019 15:35:03 +0900 Subject: [PATCH] Support qualified cursor --- src/Processor.php | 21 +++++++++++++++++++++ tests/ProcessorTest.php | 28 +++++++++++++++++++++++++++- 2 files changed, 48 insertions(+), 1 deletion(-) diff --git a/src/Processor.php b/src/Processor.php index 0bac7c2..70a08b0 100644 --- a/src/Processor.php +++ b/src/Processor.php @@ -45,6 +45,7 @@ public function process(Query $query, $rows) */ protected function field($row, $column) { + $column = $this->dropTablePrefix($column); if ($this->builder instanceof BelongsToMany && strpos($column, 'pivot_') === 0) { return $this->pivotField($row, substr($column, 6), $this->pivotAccessor()); } @@ -140,4 +141,24 @@ protected function defaultFormat($rows, array $meta, Query $query) { return new PaginationResult($rows, $meta); } + + /** + * Drop table prefix on column name. + * + * @param string $column + * @return string + */ + protected function dropTablePrefix(string $column) + { + if (!$this->builder) { + return $column; + } + + // e.g. + // x -> "x" in Standard SQL + // -> [x] in MS SQL + // -> `x` in MySQL + $q = $this->builder->getConnection()->getQueryGrammar()->wrap('x')[0]; + return preg_replace("/^(?:(?:{$q}[^{$q}]*?{$q}|\w*?)\.)*?(?:{$q}([^{$q}]*){$q}|(\w*))$/", '$1$2', $column); + } } diff --git a/tests/ProcessorTest.php b/tests/ProcessorTest.php index e7cb03f..2e1179b 100644 --- a/tests/ProcessorTest.php +++ b/tests/ProcessorTest.php @@ -437,7 +437,7 @@ public function testDescendingBackwardExclusive() /** * @test */ - public function testBelongsToMany() + public function testBelongsToManyOrderByPivot() { $this->assertResultSame( [ @@ -460,6 +460,32 @@ public function testBelongsToMany() ); } + /** + * @test + */ + public function testBelongsToManyOrderBySource() + { + $this->assertResultSame( + [ + 'records' => [ + ['id' => 2, 'updated_at' => '2017-01-01 11:00:00'], + ['id' => 3, 'updated_at' => '2017-01-01 10:00:00'], + ['id' => 4, 'updated_at' => '2017-01-01 11:00:00'], + ], + 'has_previous' => true, + 'previous_cursor' => ['posts.id' => 1], + 'has_next' => true, + 'next_cursor' => ['posts.id' => 5], + ], + Tag::find(1)->posts()->withPivot('id') + ->lampager() + ->forward()->limit(3) + ->orderBy('posts.id') + ->seekable() + ->paginate(['posts.id' => 2]) + ); + } + /** * @test */