Skip to content
Permalink
Browse files

Deleting translations when the associated records is deleted

  • Loading branch information...
lorenzo committed Feb 13, 2014
1 parent 6ba26b0 commit 9c29a7829f7dcb7b1e6d457aee64140321280725
@@ -201,6 +201,22 @@ public function beforeSave(Event $event, Entity $entity, ArrayObject $options) {
}
}
/**
* Deletes all translation for the entity that was recently deleted
*
* @param \Cake\Event\Event the afterDelete event that was fired
* @param \Cake\ORM\Entity the entity that was recently deleted
* @return void
*/
public function afterDelete(Event $event, Entity $entity) {
$primary = (array)$this->_table->primaryKey();
$key = $entity->get(current($primary));
TableRegistry::get('I18n')->deleteAll([
'foreign_key' => $key,
'model' => $this->_table->alias()
]);
}
/**
* Sets all future finds for the bound table to also fetch translated fields for
* the passed locale. If no value is passed, it returns the currently configured
@@ -1421,6 +1421,8 @@ protected function _processDelete($entity, $options) {
throw new \InvalidArgumentException($msg);
}
$this->_associated->cascadeDelete($entity, $options->getArrayCopy());
$query = $this->query();
$statement = $query->delete()
->where($conditions)
@@ -1431,8 +1433,6 @@ protected function _processDelete($entity, $options) {
return $success;
}
$this->_associated->cascadeDelete($entity, $options->getArrayCopy());
$event = new Event('Model.afterDelete', $this, [
'entity' => $entity,
'options' => $options
@@ -570,4 +570,21 @@ public function testSaveTranslationWithAssociationWhitelist() {
$this->assertEquals('Le titre', $article->get('title'));
}
/**
* Tests that after deleting a translated entity, all translations are also removed
*
* @return void
*/
public function testDelete() {
$table = TableRegistry::get('Articles');
$table->addBehavior('Translate', ['fields' => ['title', 'body']]);
$article = $table->find()->first();
$this->assertTrue($table->delete($article));
$translations = TableRegistry::get('I18n')->find()
->where(['model' => 'Articles', 'foreign_key' => $article->id])
->count();
$this->assertEquals(0, $translations);
}
}

0 comments on commit 9c29a78

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