From d989ed640a86ddb902cdccc11ce45fbfb28a0d53 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aur=C3=A9lien=20Roy?= Date: Tue, 1 Oct 2019 14:07:39 +0200 Subject: [PATCH 1/6] Add support for transOrderBy --- behaviors/TranslatableModel.php | 47 +++++++++++++++++++++++++++++---- 1 file changed, 42 insertions(+), 5 deletions(-) diff --git a/behaviors/TranslatableModel.php b/behaviors/TranslatableModel.php index 70c7b0a6..05f9fa63 100644 --- a/behaviors/TranslatableModel.php +++ b/behaviors/TranslatableModel.php @@ -23,7 +23,7 @@ class TranslatableModel extends TranslatableBehavior { /** * Applies a translatable index to a basic query. This scope will join the index - * table and cannot be executed more than once. + * table and can be executed neither more than once, nor with scopeTransOrder. * @param Builder $query * @param string $index * @param string $value @@ -32,10 +32,6 @@ class TranslatableModel extends TranslatableBehavior */ public function scopeTransWhere($query, $index, $value, $locale = null, $operator = '=') { - if (!$locale) { - $locale = $this->translatableContext; - } - $query->select($this->model->getTable().'.*'); $query->where(function($q) use ($index, $value, $operator) { @@ -48,6 +44,47 @@ public function scopeTransWhere($query, $index, $value, $locale = null, $operato }); }); + $this->joinTranslateIndexesTable($query, $locale); + + return $query; + } + + /** + * Applies a sort operation with a translatable index to a basic query. This scope will join the index + * table and can be executed neither more than once, nor with scopeTransWhere. + * @param Builder $query + * @param string $index + * @param string $direction + * @param string $locale + * @return Builder + */ + public function scopeTransOrderBy($query, $index, $direction = 'asc', $locale = null) + { + $query->select( + $this->model->getTable().'.*', + Db::raw('COALESCE(rainlab_translate_indexes.value, '. $this->model->getTable() .'.'.$index.') AS translate_sorting_key') + ); + + $query->orderBy('translate_sorting_key', $direction); + + $this->joinTranslateIndexesTable($query, $locale); + + return $query; + } + + /** + * Joins the translatable indexes table to a query. + * This cannot be executed more than once. + * @param Builder $query + * @param string $locale + * @return Builder + */ + protected function joinTranslateIndexesTable($query, $locale = null) { + + if (!$locale) { + $locale = $this->translatableContext; + } + // This join will crap out if this scope executes twice, it is a known issue. // It should check if the join exists before applying it, this mechanism was // not found in Laravel. So options are block joins entirely or allow once. From f3a7876bf61228d020e12460e7587e84c33f5a36 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aur=C3=A9lien=20Roy?= Date: Thu, 3 Oct 2019 10:11:28 +0200 Subject: [PATCH 2/6] Code reformatting --- behaviors/TranslatableModel.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/behaviors/TranslatableModel.php b/behaviors/TranslatableModel.php index 05f9fa63..a88d426b 100644 --- a/behaviors/TranslatableModel.php +++ b/behaviors/TranslatableModel.php @@ -79,8 +79,8 @@ public function scopeTransOrderBy($query, $index, $direction = 'asc', $locale = * @param string $locale * @return Builder */ - protected function joinTranslateIndexesTable($query, $locale = null) { - + protected function joinTranslateIndexesTable($query, $locale = null) + { if (!$locale) { $locale = $this->translatableContext; } From 362f98b4de8459f0e488b3683af6be1c16d609c2 Mon Sep 17 00:00:00 2001 From: Aurelien Roy Date: Wed, 30 Oct 2019 18:33:48 +0100 Subject: [PATCH 3/6] Add unit test for transOrderBy --- behaviors/TranslatableModel.php | 1 + tests/fixtures/models/Country.php | 2 +- .../unit/behaviors/TranslatableModelTest.php | 31 +++++++++++++++++++ 3 files changed, 33 insertions(+), 1 deletion(-) diff --git a/behaviors/TranslatableModel.php b/behaviors/TranslatableModel.php index a88d426b..42085eee 100644 --- a/behaviors/TranslatableModel.php +++ b/behaviors/TranslatableModel.php @@ -60,6 +60,7 @@ public function scopeTransWhere($query, $index, $value, $locale = null, $operato */ public function scopeTransOrderBy($query, $index, $direction = 'asc', $locale = null) { + $query->select( $this->model->getTable().'.*', Db::raw('COALESCE(rainlab_translate_indexes.value, '. $this->model->getTable() .'.'.$index.') AS translate_sorting_key') diff --git a/tests/fixtures/models/Country.php b/tests/fixtures/models/Country.php index fd08d0a2..5189a952 100644 --- a/tests/fixtures/models/Country.php +++ b/tests/fixtures/models/Country.php @@ -9,7 +9,7 @@ class Country extends Model { public $implement = ['@RainLab.Translate.Behaviors.TranslatableModel']; - public $translatable = ['name', 'states']; + public $translatable = [['name', 'index' => true], 'states']; /** * @var string The database table used by the model. diff --git a/tests/unit/behaviors/TranslatableModelTest.php b/tests/unit/behaviors/TranslatableModelTest.php index 2a64208f..4bdb5074 100644 --- a/tests/unit/behaviors/TranslatableModelTest.php +++ b/tests/unit/behaviors/TranslatableModelTest.php @@ -3,6 +3,7 @@ use Schema; use PluginTestCase; use Model; +use RainLab\Translate\Classes\Translator; use RainLab\Translate\Tests\Fixtures\Models\Country as CountryModel; use RainLab\Translate\Models\Locale as LocaleModel; @@ -101,4 +102,34 @@ public function testSetTranslationValue() $this->assertEquals(['a', 'b', 'c'], $obj->states); } + public function testOrderBy() + { + $locale = Translator::instance()->getLocale(); + $this->recycleSampleData(); + + $obj = CountryModel::first(); + + $obj->translateContext('fr'); + $obj->name = 'Australie'; + $obj->save(); + + $obj = CountryModel::create([ + 'name' => 'Germany', + 'code' => 'DE' + ]); + + $obj->translateContext('fr'); + $obj->name = 'Allemagne'; + $obj->save(); + + $res = CountryModel::transOrderBy('name')->get()->pluck('name'); + $this->assertEquals(['Australia', 'Germany'], $res->toArray()); + + Translator::instance()->setLocale('fr'); + $res = CountryModel::transOrderBy('name')->get()->pluck('name'); + $this->assertEquals(['Allemagne', 'Australie'], $res->toArray()); + + Translator::instance()->setLocale('en'); + } + } From 54e955b39d663155633d9e751ba86203bddcb525 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aur=C3=A9lien=20Roy?= Date: Wed, 30 Oct 2019 18:35:26 +0100 Subject: [PATCH 4/6] Code format --- behaviors/TranslatableModel.php | 1 - 1 file changed, 1 deletion(-) diff --git a/behaviors/TranslatableModel.php b/behaviors/TranslatableModel.php index 42085eee..a88d426b 100644 --- a/behaviors/TranslatableModel.php +++ b/behaviors/TranslatableModel.php @@ -60,7 +60,6 @@ public function scopeTransWhere($query, $index, $value, $locale = null, $operato */ public function scopeTransOrderBy($query, $index, $direction = 'asc', $locale = null) { - $query->select( $this->model->getTable().'.*', Db::raw('COALESCE(rainlab_translate_indexes.value, '. $this->model->getTable() .'.'.$index.') AS translate_sorting_key') From c120f91286176e7bd5b9e514253670e2bdc98f63 Mon Sep 17 00:00:00 2001 From: Aurelien Roy Date: Wed, 30 Oct 2019 20:00:20 +0100 Subject: [PATCH 5/6] Add unit test for transWhere --- .../unit/behaviors/TranslatableModelTest.php | 22 +++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/tests/unit/behaviors/TranslatableModelTest.php b/tests/unit/behaviors/TranslatableModelTest.php index 4bdb5074..55e7c13d 100644 --- a/tests/unit/behaviors/TranslatableModelTest.php +++ b/tests/unit/behaviors/TranslatableModelTest.php @@ -102,9 +102,27 @@ public function testSetTranslationValue() $this->assertEquals(['a', 'b', 'c'], $obj->states); } - public function testOrderBy() + public function testTranslateWhere() + { + $this->recycleSampleData(); + + $obj = CountryModel::first(); + + $obj->translateContext('fr'); + $obj->name = 'Australie'; + $obj->save(); + + $this->assertEquals(0, CountryModel::transWhere('name', 'Australie')->count()); + + Translator::instance()->setLocale('fr'); + $this->assertEquals(1, CountryModel::transWhere('name', 'Australi')->count()); + + Translator::instance()->setLocale('en'); + + } + + public function testTranslateOrderBy() { - $locale = Translator::instance()->getLocale(); $this->recycleSampleData(); $obj = CountryModel::first(); From 8faafd50f32c79b773a910a86e78b41a5ae795a9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aur=C3=A9lien=20Roy?= Date: Thu, 31 Oct 2019 12:00:48 +0100 Subject: [PATCH 6/6] Fix typo --- tests/unit/behaviors/TranslatableModelTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/unit/behaviors/TranslatableModelTest.php b/tests/unit/behaviors/TranslatableModelTest.php index 55e7c13d..bfac5606 100644 --- a/tests/unit/behaviors/TranslatableModelTest.php +++ b/tests/unit/behaviors/TranslatableModelTest.php @@ -115,7 +115,7 @@ public function testTranslateWhere() $this->assertEquals(0, CountryModel::transWhere('name', 'Australie')->count()); Translator::instance()->setLocale('fr'); - $this->assertEquals(1, CountryModel::transWhere('name', 'Australi')->count()); + $this->assertEquals(1, CountryModel::transWhere('name', 'Australie')->count()); Translator::instance()->setLocale('en');