Fix inconsistent name/alias usage.

TranslateBehavior should always use name instead of alias when
creating/updating/saving new translate records. It already uses name
when finding translations and the mismatch was causing translations to
not be found when saved from an aliased model.

Thanks to Joost de Keijzer for providing the initial patch.

Fixes #3865
This commit is contained in:
mark_story 2013-06-02 13:48:17 -04:00
parent a6bc92719a
commit cbf3228c34
2 changed files with 33 additions and 3 deletions

View file

@ -417,7 +417,7 @@ class TranslateBehavior extends ModelBehavior {
}
unset($this->runtime[$Model->alias]['beforeValidate'], $this->runtime[$Model->alias]['beforeSave']);
$conditions = array('model' => $Model->alias, 'foreign_key' => $Model->id);
$conditions = array('model' => $Model->name, 'foreign_key' => $Model->id);
$RuntimeModel = $this->translateModel($Model);
if ($created) {
@ -502,7 +502,7 @@ class TranslateBehavior extends ModelBehavior {
*/
public function afterDelete(Model $Model) {
$RuntimeModel = $this->translateModel($Model);
$conditions = array('model' => $Model->alias, 'foreign_key' => $Model->id);
$conditions = array('model' => $Model->name, 'foreign_key' => $Model->id);
$RuntimeModel->deleteAll($conditions);
}
@ -611,7 +611,7 @@ class TranslateBehavior extends ModelBehavior {
}
}
$associations[$association] = array_merge($default, array('conditions' => array(
'model' => $Model->alias,
'model' => $Model->name,
$RuntimeModel->displayField => $field
)));
}

View file

@ -531,6 +531,36 @@ class TranslateBehaviorTest extends CakeTestCase {
$this->assertEquals($expected, $result);
}
/**
* test saving/deleting with an alias, uses the model name.
*
* @return void
*/
public function testSaveDeleteIgnoreAlias() {
$this->loadFixtures('Translate', 'TranslatedItem');
$TestModel = new TranslatedItem(array('alias' => 'SomethingElse'));
$TestModel->locale = 'spa';
$data = array(
'slug' => 'fourth_translated',
'title' => 'Leyenda #4',
'content' => 'Contenido #4',
'translated_article_id' => 1,
);
$TestModel->create($data);
$TestModel->save();
$id = $TestModel->id;
$result = $TestModel->read();
$expected = array($TestModel->alias => array_merge($data, array('id' => $id, 'locale' => 'spa')));
$this->assertEquals($expected, $result);
$TestModel->delete($id);
$result = $TestModel->translateModel()->find('count', array(
'conditions' => array('foreign_key' => $id)
));
$this->assertEquals(0, $result);
}
/**
* test save multiple locales method
*