Skip to content

Commit

Permalink
Update afterSave to ensure created entires have all translated fields…
Browse files Browse the repository at this point in the history
… present

Without all fields being present, find() will be unable to find the
translated records.

Fixes #3009
  • Loading branch information
darxmac authored and markstory committed Jul 7, 2012
1 parent ed4493d commit 1c0b6c0
Showing 1 changed file with 10 additions and 0 deletions.
10 changes: 10 additions & 0 deletions lib/Cake/Model/Behavior/TranslateBehavior.php
Expand Up @@ -392,6 +392,16 @@ public function afterSave(Model $model, $created) {
unset($this->runtime[$model->alias]['beforeValidate'], $this->runtime[$model->alias]['beforeSave']);
$conditions = array('model' => $model->alias, 'foreign_key' => $model->id);
$RuntimeModel = $this->translateModel($model);

$fields = array_merge($this->settings[$model->alias], $this->runtime[$model->alias]['fields']);
if ($created) {
foreach ($fields as $field) {
if (!isset($tempData[$field])) {
//set the field value to an empty string
$tempData[$field] = '';
}
}
}

foreach ($tempData as $field => $value) {
unset($conditions['content']);
Expand Down

6 comments on commit 1c0b6c0

@grobux
Copy link

@grobux grobux commented on 1c0b6c0 Jul 19, 2012

Choose a reason for hiding this comment

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

Hi,

With that Translate in my model:

'Translate' => array(
  'title' => 'titleTranslation'
)

With 2 (or more) languages (EN, FR, etc...), when I add a record in my back-office, I have 2 SQL inserts (field "title" for EN, field "title" for FR, etc...).
Nothing wrong here.

But after that, I have also one SQL update for the field "titleTranslation" for my default language, which content is empty.
So I lose the "title" field for my website default language.

To sum up: for 2 locales, I have 2 records in my I18n table: one with the classic name, and one with the Translate field. So i loose the record of my default language.
This problem is linked to that commit.

@darxmac
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Doesn't make sense, the patch only fires when $created is set ... so it shouldn't do anything on updates ?

@markstory
Copy link
Member

Choose a reason for hiding this comment

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

Why would you do it on updates? That could potentially cause a partial save to erase all previous translations that defined other fields. Changing this code to apply on updates would make partial updates impossible.

@markstory
Copy link
Member

Choose a reason for hiding this comment

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

I'm a dork, I misread your comment @darxmac, sorry about that. But yes this code only applies to creates.

@grobux
Copy link

@grobux grobux commented on 1c0b6c0 Jul 19, 2012

Choose a reason for hiding this comment

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

Thank you for your attention and responsiveness.
Yes, my problem only happen when I add a record.

In my case, it adds an i18n record for "EN" and "FR" for the field "title", and then it adds an empty value for "titleTranslation".

Here is a dump of $tempData after your code:

array
  'title' => 
    array
      'FR' => string 'Mon contenu' (length=5)
      'EN' => string 'My content' (length=5)
  'titleTranslation' => string '' (length=0)

Here is a dump of SQL queries executed around that piece of code:

INSERT INTO `bdd`.`tables` (`title`) VALUES ('My content');
INSERT INTO `bdd`.`i18n` (`model`, `foreign_key`, `field`, `locale`, `content`) VALUES ('Table', 8, 'title', 'FR', 'Mon contenu');
INSERT INTO `bdd`.`i18n` (`model`, `foreign_key`, `field`, `locale`, `content`) VALUES ('Table', 8, 'title', 'EN', 'My content');
SELECT `I18nModel`.`locale`, `I18nModel`.`id` FROM `bdd`.`i18n` AS `I18nModel` WHERE `model` = 'Table' AND `foreign_key` = 8 AND `field` = 'titleTranslation' AND `locale` = 'EN';
UPDATE `bdd`.`i18n` SET `model` = 'Table', `foreign_key` = 8, `field` = 'titleTranslation', `locale` = 'EN', `content` = '', `id` = 46  WHERE `bdd`.`i18n`.`id` = '46';

I'm looking on my side if it's a generic problem or my fault/misunderstand.
Perhaps a conflict with the default locale...

I look further and try to find a solution if needed.
Thanks again for your help and work.

@markstory
Copy link
Member

Choose a reason for hiding this comment

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

@ grobux This was fixed after 2.2.1 and will be in 2.2.2. You can either upgrade to the current master branch for your app, or wait for 2.2.2 to be released, it shouldn't be too long now.

Please sign in to comment.