Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Starting to implement multiple translations saving
  • Loading branch information
lorenzo committed Feb 15, 2014
1 parent fa77f26 commit 9499b17
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 5 deletions.
49 changes: 46 additions & 3 deletions src/Model/Behavior/TranslateBehavior.php
Expand Up @@ -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()));
Expand Down Expand Up @@ -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);
}
}

}
2 changes: 0 additions & 2 deletions src/ORM/Association/DependentDeleteTrait.php
@@ -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)
*
Expand Down
12 changes: 12 additions & 0 deletions tests/TestCase/Model/Behavior/TranslateBehaviorTest.php
Expand Up @@ -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.