Skip to content

always fill "translations" relations (skip translation) #216

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

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all 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
7 changes: 7 additions & 0 deletions src/Translatable/Translatable.php
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,11 @@ protected function saveTranslations(): bool
}
}

// Fresh "translation" relation if loaded
if ($saved && $this->relationLoaded('translation')) {
$this->load('translation');
}

return $saved;
}

Expand Down Expand Up @@ -428,6 +433,8 @@ protected function getTranslationByLocaleKey(string $key): ?Model
$this->relationLoaded('translation')
&& $this->translation
&& $this->translation->getAttribute($this->getLocaleKey()) == $key
// skip when called from fill method
&& (debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS)[3]['function'] ?? null) !== 'fill'
) {
return $this->translation;
}
Expand Down
48 changes: 48 additions & 0 deletions tests/Eloquent/CountryWithEarlyLoad.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

namespace Astrotomic\Translatable\Tests\Eloquent;

use Astrotomic\Translatable\Contracts\Translatable as TranslatableContract;
use Astrotomic\Translatable\Translatable;
use Illuminate\Database\Eloquent\Model as Eloquent;

class CountryWithEarlyLoad extends Eloquent implements TranslatableContract
{
use Translatable;

protected $table = 'countries';

/**
* Array with the fields translated in the Translation table.
*
* @var array
*/
public $translatedAttributes = ['name'];

protected $translationForeignKey = 'country_id';

/**
* Set $translationModel if you want to overwrite the convention
* for the name of the translation Model. Use full namespace if applied.
*
* The convention is to add "Translation" to the name of the class extending Translatable.
* Example: Country => CountryTranslation
*/
public $translationModel = CountryTranslation::class;

/**
* Add your translated attributes here if you want
* fill them with mass assignment.
*
* @var array
*/
public $fillable = [];
public $guarded = [];

/**
* The relations to eager load on every query.
*
* @var array
*/
protected $with = ['translation'];
}
75 changes: 75 additions & 0 deletions tests/TranslatableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Astrotomic\Translatable\Tests\Eloquent\Country;
use Astrotomic\Translatable\Tests\Eloquent\CountryStrict;
use Astrotomic\Translatable\Tests\Eloquent\CountryTranslation;
use Astrotomic\Translatable\Tests\Eloquent\CountryWithEarlyLoad;
use Astrotomic\Translatable\Tests\Eloquent\Person;
use Astrotomic\Translatable\Tests\Eloquent\Vegetable;
use Astrotomic\Translatable\Tests\Eloquent\VegetableTranslation;
Expand Down Expand Up @@ -957,6 +958,80 @@ public function it_uses_translation_relation_if_locale_matches(): void
static::assertFalse($country->relationLoaded('translations'));
}

/** @test */
public function it_update_all_translated_locales_when_translation_relation_is_loaded(): void
{
$this->app->make('config')->set('translatable.locales', ['de', 'en']);
$this->app->setLocale('de');
$this->app->make(Locales::class)->load();

// First create country
CountryWithEarlyLoad::create([
'id' => 100,
'code' => 'my',
'de' => [
'name' => 'Deutschland',
],
'en' => [
'name' => 'Germany',
],
]);

$country = CountryWithEarlyLoad::find(100);

// try mass update
$country->update([
'code' => 'my',
'de' => [
'name' => 'New Deutschland',
],
'en' => [
'name' => 'New Germany',
],
]);

$country = CountryWithEarlyLoad::find(100);

static::assertEquals(100, $country->getKey());
static::assertEquals('New Deutschland', $country->getTranslation('de', false)->name);
static::assertEquals('New Germany', $country->getTranslation('en', false)->name);
}

/** @test */
public function it_fresh_translation_relation_after_successful_save_translations()
{
$this->app->make('config')->set('translatable.locales', ['de', 'en']);
$this->app->setLocale('de');
$this->app->make(Locales::class)->load();

// First create country
CountryWithEarlyLoad::create([
'id' => 100,
'code' => 'my',
'de' => [
'name' => 'Deutschland',
],
'en' => [
'name' => 'Germany',
],
]);

$country = CountryWithEarlyLoad::find(100);

// try mass update
$country->update([
'code' => 'my',
'de' => [
'name' => 'New Deutschland',
],
'en' => [
'name' => 'New Germany',
],
]);

static::assertEquals('New Deutschland', $country->translation->name);
}

/** @test */
public function it_uses_translations_relation_if_locale_does_not_match(): void
{
Expand Down