Skip to content

Commit

Permalink
Fixes saving with multiple context
Browse files Browse the repository at this point in the history
Fixes #745

Test code:

```
$s = rand();

$c = October\Test\Models\Country::find(1);
$c->name = 'english'.$s;

$c->translateContext('fr');
$c->name = 'french'.$s;

$c->translateContext('de');
$c->name = 'german'.$s;

$c->save();
```
  • Loading branch information
octoberapp committed Aug 1, 2024
1 parent 10b2ff6 commit 876f6b1
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions behaviors/TranslatableModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -297,9 +297,13 @@ protected function storeTranslatableData($locale = null)
*/
protected function storeTranslatableBasicData($locale = null)
{
if (!$locale) {
$locale = $this->translatableContext;
}

$data = (array) $this->translatableAttributes[$locale];

$data = $this->getUniqueTranslatableData($data);
$data = $this->getUniqueTranslatableData($locale, $data);

$data = json_encode($data, JSON_UNESCAPED_UNICODE);

Expand All @@ -326,10 +330,14 @@ protected function storeTranslatableBasicData($locale = null)
* by leveraging originalIsEquivalent. It applies contextual values from
* setAttributeTranslated, in addition to attributes set on the model.
*/
protected function getUniqueTranslatableData(array $data): array
protected function getUniqueTranslatableData($locale, array $data): array
{
$originalContext = $this->model->translateContext();

$originalAttrs = $this->model->attributes;

$this->model->translateContext($locale);

$this->model->forceFill($data);

// Only include attributes that are different from the parent
Expand All @@ -346,6 +354,8 @@ protected function getUniqueTranslatableData(array $data): array

$data = array_intersect_key($data, $includeAttrs);

$this->model->translateContext($originalContext);

$this->model->attributes = $originalAttrs;

return $data;
Expand Down

0 comments on commit 876f6b1

Please sign in to comment.