Skip to content
This repository has been archived by the owner on Jun 18, 2019. It is now read-only.

Commit

Permalink
toArray() performance (#315)
Browse files Browse the repository at this point in the history
* Skip translations in toArray() when configured (see #275)

* fix styles
  • Loading branch information
dimsav committed Jan 25, 2017
1 parent 685ee5a commit 24a2d1b
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 0 deletions.
1 change: 1 addition & 0 deletions changelog.md
Expand Up @@ -6,6 +6,7 @@
```
$country->fill(['name:en' => 'Belgium']);
```
- Added config to skip translations in toArray() for better performance when needed. #315

### v. 6.0.1

Expand Down
4 changes: 4 additions & 0 deletions readme.md
Expand Up @@ -178,6 +178,10 @@ With this command, initialize the configuration and modify the created file, loc

## Configuration

### The config file

You can see the options for further customization in the [config file](src/config/translatable.php).

### The translation model

The convention used to define the class of the translation model is to append the keyword `Translation`.
Expand Down
14 changes: 14 additions & 0 deletions src/Translatable/Translatable.php
Expand Up @@ -586,6 +586,12 @@ public function toArray()
{
$attributes = parent::toArray();

if ($this->relationLoaded('translations') || $this->toArrayAlwaysLoadsTranslations()) {
// continue
} else {
return $attributes;
}

$hiddenAttributes = $this->getHidden();

foreach ($this->translatedAttributes as $field) {
Expand Down Expand Up @@ -651,4 +657,12 @@ private function getAttributeAndLocale($key)

return [$key, $this->locale()];
}

/**
* @return bool
*/
private function toArrayAlwaysLoadsTranslations()
{
return app()->make('config')->get('translatable.to_array_always_loads_translations', true);
}
}
10 changes: 10 additions & 0 deletions src/config/translatable.php
Expand Up @@ -91,4 +91,14 @@
*/
'locale_key' => 'locale',

/*
|--------------------------------------------------------------------------
| Always load translations when converting to array
|--------------------------------------------------------------------------
| Setting this to false will have a performance improvement but will
| not return the translations when using toArray(), unless the
| translations relationship is already loaded.
|
*/
'to_array_always_loads_translations' => true,
];
14 changes: 14 additions & 0 deletions tests/TranslatableTest.php
Expand Up @@ -435,6 +435,20 @@ public function test_to_array_and_fallback_with_country_based_locales_enabled()
$this->assertSame('frites', $fritesArray['name']);
}

public function test_it_skips_translations_in_to_array_when_config_is_set()
{
$this->app->config->set('translatable.to_array_always_loads_translations', false);
$greece = Country::whereCode('gr')->first()->toArray();
$this->assertFalse(isset($greece['name']));
}

public function test_it_returns_translations_in_to_array_when_config_is_set_but_translations_are_loaded()
{
$this->app->config->set('translatable.to_array_always_loads_translations', false);
$greece = Country::whereCode('gr')->with('translations')->first()->toArray();
$this->assertTrue(isset($greece['name']));
}

public function test_it_should_mutate_the_translated_attribute_if_a_mutator_is_set_on_model()
{
$person = new Person(['name' => 'john doe']);
Expand Down

0 comments on commit 24a2d1b

Please sign in to comment.