Skip to content
This repository
Browse code

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...
commit 1c0b6c076a15516688df5c6474392fa8e79bb010 1 parent ed4493d
Thomas von Hassel authored July 06, 2012 markstory committed July 07, 2012
10  lib/Cake/Model/Behavior/TranslateBehavior.php
@@ -392,6 +392,16 @@ public function afterSave(Model $model, $created) {
392 392
 		unset($this->runtime[$model->alias]['beforeValidate'], $this->runtime[$model->alias]['beforeSave']);
393 393
 		$conditions = array('model' => $model->alias, 'foreign_key' => $model->id);
394 394
 		$RuntimeModel = $this->translateModel($model);
  395
+	
  396
+		$fields = array_merge($this->settings[$model->alias], $this->runtime[$model->alias]['fields']);
  397
+		if ($created) {
  398
+			foreach ($fields as $field) {
  399
+				if (!isset($tempData[$field])) {
  400
+					//set the field value to an empty string
  401
+					$tempData[$field] = '';
  402
+				}
  403
+			}
  404
+		}
395 405
 
396 406
 		foreach ($tempData as $field => $value) {
397 407
 			unset($conditions['content']);

8 notes on commit 1c0b6c0

grobux

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.

Thomas von Hassel

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

Mark Story
Owner

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.

Mark Story
Owner

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

grobux

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
513 commented on 1c0b6c0 July 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
513 commented on 1c0b6c0 July 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 ?

Mark Story
Owner

@ 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.
Something went wrong with that request. Please try again.