Permalink
Browse files

Update afterSave to ensure created entires have all translated fields…

… present

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

Fixes #3009
  • Loading branch information...
1 parent ed4493d commit 1c0b6c076a15516688df5c6474392fa8e79bb010 @darxmac darxmac committed with markstory Jul 6, 2012
Showing with 10 additions and 0 deletions.
  1. +10 −0 lib/Cake/Model/Behavior/TranslateBehavior.php
@@ -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']);

8 comments on commit 1c0b6c0

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.

Contributor

darxmac replied Jul 19, 2012

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

Owner

markstory replied Jul 19, 2012

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.

Owner

markstory replied Jul 19, 2012

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

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.

513 replied Jul 31, 2012

Hello,
I understood the behavior of this patch, but why it uses the Translate's array configuration value 'titleTranslation' instead the key 'title' to be in the fieldname "field" of the translation table ?

513 replied Jul 31, 2012

Hello,
I understood the behavior of this patch, but why it uses the Translate's array configuration value 'titleTranslation' instead the key 'title' to be in the fieldname "field" of the translation table ?

Owner

markstory replied Jul 31, 2012

@ 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.