Permalink
Browse files

Removing the _i18n property after saving

  • Loading branch information...
1 parent c602589 commit 3b4f69db5bf6b63f8130267197daadbcf740f275 @lorenzo lorenzo committed Feb 15, 2014
@@ -206,6 +206,17 @@ public function beforeSave(Event $event, Entity $entity, ArrayObject $options) {
}
}
+/**
+ * Unsets the temporary `_i18n` property after the entity has been saved
+ *
+ * @param \Cake\Event\Event the beforeSave event that was fired
+ * @param \Cake\ORM\Entity the entity that is going to be saved
+ * @return void
+ */
+ public function afterSave(Event $event, Entity $entity) {
+ $entity->unsetProperty('_i18n');
+ }
+
/**
* Deletes all translation for the entity that was recently deleted
*
@@ -270,9 +281,7 @@ public function findTranslations($query, $options) {
}
return $q;
}])
- ->formatResults(function($results) {
- return $this->_groupTranslations($results);
- }, $query::PREPEND);
+ ->formatResults([$this, 'groupTranslations'], $query::PREPEND);
}
/**
@@ -316,7 +325,7 @@ protected function _rowMapper($results, $locale) {
* @param \Cake\ORM\ResultSetDecorator $results
* @return \Cake\Collection\Collection
*/
- protected function _groupTranslations($results) {
+ public function groupTranslations($results) {
return $results->map(function($row) {
$translations = (array)$row->get('_i18n');
$grouped = new Collection($translations);
@@ -470,6 +470,7 @@ public function testUpdateSingleLocale() {
$this->assertEquals(1, $article->get('id'));
$article->set('title', 'New translated article');
$table->save($article);
+ $this->assertNull($article->get('_i18n'));
$article = $table->find()->first();
$this->assertEquals(1, $article->get('id'));
@@ -485,6 +486,7 @@ public function testUpdateSingleLocale() {
$article->set('title', 'Wow, such translated article');
$article->set('body', 'A translated body');
$table->save($article);
+ $this->assertNull($article->get('_i18n'));
$article = $table->find()->first();
$this->assertEquals(1, $article->get('id'));
@@ -516,6 +518,7 @@ public function testInsertNewTranslations() {
$article->set('title', 'Un autre titre');
$article->set('body', 'Le contenu');
$table->save($article);
+ $this->assertNull($article->get('_i18n'));
$article = $table->find()->first();
$this->assertEquals('Un autre titre', $article->get('title'));
@@ -536,6 +539,7 @@ public function testUpdateTranslationWithLocaleInEntity() {
$article->set('_locale', 'fra');
$article->set('title', 'Le titre');
$table->save($article);
+ $this->assertNull($article->get('_i18n'));
$article = $table->find()->first();
$this->assertEquals(1, $article->get('id'));
@@ -565,6 +569,7 @@ public function testSaveTranslationWithAssociationWhitelist() {
$this->assertEquals(1, $article->get('id'));
$article->set('title', 'Le titre');
$table->save($article, ['associated' => ['Comments']]);
+ $this->assertNull($article->get('_i18n'));
$article = $table->find()->first();
$this->assertEquals('Le titre', $article->get('title'));
@@ -603,6 +608,7 @@ public function testSaveMultipleTranslations() {
$translations['eng']->set('body', 'Another body');
$article->set('_translations', $translations);
$table->save($article);
+ $this->assertNull($article->get('_i18n'));
$article = $results = $table->find('translations')->first();
$translations = $article->get('_translations');
@@ -627,6 +633,7 @@ public function testSaveMultipleNewTranslations() {
$translations['fre'] = new Entity(['title' => 'Titre']);
$article->set('_translations', $translations);
$table->save($article);
+ $this->assertNull($article->get('_i18n'));
$article = $results = $table->find('translations')->first();
$translations = $article->get('_translations');

0 comments on commit 3b4f69d

Please sign in to comment.