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

toArray() performance #315

Merged
merged 4 commits into from
Jan 25, 2017
Merged
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
1 change: 1 addition & 0 deletions changelog.md
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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