Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for transOrderBy #516

Merged
merged 9 commits into from
Feb 28, 2020
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 42 additions & 5 deletions behaviors/TranslatableModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mjauvin what does this mean?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed... @aurelien-roy can you fix this comment so it makes sense?

* @param Builder $query
* @param string $index
* @param string $value
Expand All @@ -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) {
Expand All @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion tests/fixtures/models/Country.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
49 changes: 49 additions & 0 deletions tests/unit/behaviors/TranslatableModelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -101,4 +102,52 @@ public function testSetTranslationValue()
$this->assertEquals(['a', 'b', 'c'], $obj->states);
}

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());

aurelien-roy marked this conversation as resolved.
Show resolved Hide resolved
Translator::instance()->setLocale('en');

}

public function testTranslateOrderBy()
{
$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');
}

}