fixes #4165, string conditions with translate behavior

git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@6552 3807eeeb-6ff5-0310-8944-8be069107fe0
This commit is contained in:
gwoo 2008-03-10 07:01:01 +00:00
parent bacb74a6ef
commit 57a61f8f82
2 changed files with 41 additions and 12 deletions

View file

@ -167,10 +167,15 @@ class TranslateBehavior extends ModelBehavior {
'I18n__'.$field.'.'.$RuntimeModel->displayField => $field
)
);
if (is_string($query['conditions'])) {
$query['conditions'] = $db->conditions($query['conditions'], true, false) . ' AND '.$db->name('I18n__'.$field.'.locale').' = \''.$locale.'\'';
} else {
$query['conditions'][$db->name('I18n__'.$field.'.locale')] = $locale;
}
}
}
}
if (is_array($query['fields'])) {
$query['fields'] = array_merge($query['fields']);
}

View file

@ -88,7 +88,7 @@ class TranslateTest extends CakeTestCase {
$expected = array('TranslatedItem' => array('id' => 1, 'slug' => 'first_translated'));
$this->assertEqual($result, $expected);
$result = $this->Model->findAll(null, array('slug'));
$result = $this->Model->find('all', array('fields' => array('slug')));
$expected = array(
array('TranslatedItem' => array('slug' => 'first_translated')),
array('TranslatedItem' => array('slug' => 'second_translated')),
@ -118,7 +118,7 @@ class TranslateTest extends CakeTestCase {
$this->Model->hasMany['Title']['fields'] = $this->Model->hasMany['Content']['fields'] = array('content');
$this->Model->hasMany['Title']['conditions']['locale'] = $this->Model->hasMany['Content']['conditions']['locale'] = 'eng';
$result = $this->Model->findAll(null, array('TranslatedItem.slug'));
$result = $this->Model->find('all', array('fields' => array('TranslatedItem.slug')));
$expected = array(
array('TranslatedItem' => array('id' => 1, 'slug' => 'first_translated'),
'Title' => array(array('foreign_key' => 1, 'content' => 'Title #1')),
@ -150,7 +150,7 @@ class TranslateTest extends CakeTestCase {
'content' => 'Content #1'));
$this->assertEqual($result, $expected);
$result = $this->Model->findAll();
$result = $this->Model->find('all');
$expected = array(
array('TranslatedItem' => array(
'id' => 1,
@ -173,6 +173,30 @@ class TranslateTest extends CakeTestCase {
$this->assertEqual($result, $expected);
}
function testLocaleSingleWithConditions() {
$this->Model->locale = 'eng';
$result = $this->Model->find('all', array('conditions' => array('slug' => 'first_translated')));
$expected = array(
array('TranslatedItem' => array(
'id' => 1,
'slug' => 'first_translated',
'locale' => 'eng',
'title' => 'Title #1',
'content' => 'Content #1')));
$this->assertEqual($result, $expected);
$result = $this->Model->find('all', array('conditions' => "TranslatedItem.slug = 'first_translated'"));
$expected = array(
array('TranslatedItem' => array(
'id' => 1,
'slug' => 'first_translated',
'locale' => 'eng',
'title' => 'Title #1',
'content' => 'Content #1')));
$this->assertEqual($result, $expected);
}
function testLocaleSingleAssociations() {
$this->Model->locale = 'eng';
$this->Model->unbindTranslation();
@ -200,7 +224,7 @@ class TranslateTest extends CakeTestCase {
$this->Model->hasMany['Title']['fields'] = $this->Model->hasMany['Content']['fields'] = array('content');
$this->Model->hasMany['Title']['conditions']['locale'] = $this->Model->hasMany['Content']['conditions']['locale'] = 'eng';
$result = $this->Model->findAll(null, array('TranslatedItem.title'));
$result = $this->Model->find('all', array('fields' => array('TranslatedItem.title')));
$expected = array(
array('TranslatedItem' => array('id' => 1, 'locale' => 'eng', 'title' => 'Title #1'),
'Title' => array(array('foreign_key' => 1, 'content' => 'Title #1')),
@ -243,7 +267,7 @@ class TranslateTest extends CakeTestCase {
'content' => 'Content #1'));
$this->assertEqual($result, $expected);
$result = $this->Model->findAll(null, array('slug', 'title', 'content'));
$result = $this->Model->find('all', array('fields' => array('slug', 'title', 'content')));
$expected = array(
array('TranslatedItem' => array(
'slug' => 'first_translated',
@ -273,21 +297,21 @@ class TranslateTest extends CakeTestCase {
function testReadSelectedFields() {
$this->Model->locale = 'eng';
$result = $this->Model->findAll(null, array('slug', 'TranslatedItem.content'));
$result = $this->Model->find('all', array('fields' => array('slug', 'TranslatedItem.content')));
$expected = array(
array('TranslatedItem' => array('slug' => 'first_translated', 'locale' => 'eng', 'content' => 'Content #1')),
array('TranslatedItem' => array('slug' => 'second_translated', 'locale' => 'eng', 'content' => 'Content #2')),
array('TranslatedItem' => array('slug' => 'third_translated', 'locale' => 'eng', 'content' => 'Content #3')));
$this->assertEqual($result, $expected);
$result = $this->Model->findAll(null, array('TranslatedItem.slug', 'content'));
$result = $this->Model->find('all', array('fields' => array('TranslatedItem.slug', 'content')));
$this->assertEqual($result, $expected);
$this->Model->locale = array('eng', 'deu', 'cze');
$delete = array(array('locale' => 'deu'), array('field' => 'content', 'locale' => 'eng'));
$this->I18nModel->deleteAll(array('or' => $delete));
$result = $this->Model->findAll(null, array('title', 'content'));
$result = $this->Model->find('all', array('fields' => array('title', 'content')));
$expected = array(
array('TranslatedItem' => array('locale' => 'eng', 'title' => 'Title #1', 'content' => 'Obsah #1')),
array('TranslatedItem' => array('locale' => 'eng', 'title' => 'Title #2', 'content' => 'Obsah #2')),