Skip to content
Permalink
Browse files

Starting to implement multiple translations saving

  • Loading branch information...
lorenzo committed Feb 15, 2014
1 parent fa77f26 commit 9499b1716c46d6d4419a443addca86c941d072aa
@@ -162,14 +162,16 @@ public function beforeFind(Event $event, $query) {
*/
public function beforeSave(Event $event, Entity $entity, ArrayObject $options) {
$locale = $entity->get('_locale') ?: $this->locale();
$table = $this->config()['translationTable'];
$newOptions = [$table => ['validate' => false]];
$options['associated'] = $newOptions + $options['associated'];
$this->_bundleTranslatedFields($entity);
if (!$locale) {
return;
}
$table = $this->config()['translationTable'];
$newOptions = [$table => ['validate' => false]];
$options['associated'] = $newOptions + $options['associated'];
$values = $entity->extract($this->config()['fields'], true);
$fields = array_keys($values);
$key = $entity->get(current((array)$this->_table->primaryKey()));
@@ -337,4 +339,45 @@ protected function _groupTranslations($results) {
});
}
protected function _bundleTranslatedFields($entity) {
$translations = (array)$entity->get('_translations');
if (empty($translations)) {
return;
}
$fields = $this->config()['fields'];
$primaryKey = (array)$this->_table->primaryKey();
$key = $entity->get(current($primaryKey));
$find = [];
foreach ($translations as $lang => $translation) {
foreach ($fields as $field) {
if ($translation->dirty($field)) {
$find[] = [
'locale' => $lang,
'field' => $field,
'foreign_key' => $key
];
$contents[] = $translation->get($field);
}
}
}
if (empty($find)) {
return;
}
$association = $this->_table->association($this->config()['translationTable']);
$query = $association->find()->select('id')->where(array_shift($find));
foreach ($find as $conditions) {
$q = $association->find()->select('id')->where(array_shift($find));
$query->unionAll($q);
}
foreach ($query->hydrate(false)->bufferResults(false) as $row) {
debug($row);
}
}
}
@@ -1,7 +1,5 @@
<?php
/**
* PHP Version 5.4
*
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
*
@@ -587,4 +587,16 @@ public function testDelete() {
$this->assertEquals(0, $translations);
}
public function testSaveMultipleTranslations() {
$table = TableRegistry::get('Articles');
$table->addBehavior('Translate', ['fields' => ['title', 'body']]);
$article = $results = $table->find('translations')->first();
$translations = $article->get('_translations');
$translations['deu']->set('title', 'Another title');
$translations['eng']->set('body', 'Another body');
$article->set('_translations', $translations);
$table->save($article);
}
}

0 comments on commit 9499b17

Please sign in to comment.
You can’t perform that action at this time.