diff --git a/lib/Cake/Model/Behavior/TranslateBehavior.php b/lib/Cake/Model/Behavior/TranslateBehavior.php index 39f84d663..88d9018c9 100644 --- a/lib/Cake/Model/Behavior/TranslateBehavior.php +++ b/lib/Cake/Model/Behavior/TranslateBehavior.php @@ -60,6 +60,25 @@ class TranslateBehavior extends ModelBehavior { * $config could be empty - and translations configured dynamically by * bindTranslation() method * + * By default INNER joins are used to fetch translations. In order to use + * other join types $config should contain 'joinType' key: + * ``` + * array( + * 'fields' => array('field_one', 'field_two' => 'FieldAssoc', 'field_three'), + * 'joinType' => 'LEFT', + * ) + * ``` + * In a model it may be configured this way: + * ``` + * public $actsAs = array( + * 'Translate' => array( + * 'content', + * 'title', + * 'joinType' => 'LEFT', + * ), + * ); + * ``` + * * @param Model $Model Model the behavior is being attached to. * @param array $config Array of configuration information. * @return mixed @@ -75,7 +94,14 @@ class TranslateBehavior extends ModelBehavior { } $this->settings[$Model->alias] = array(); - $this->runtime[$Model->alias] = array('fields' => array()); + $this->runtime[$Model->alias] = array( + 'fields' => array(), + 'joinType' => 'INNER', + ); + if (isset($config['joinType'])) { + $this->runtime[$Model->alias]['joinType'] = $config['joinType']; + unset($config['joinType']); + } $this->translateModel($Model); return $this->bindTranslation($Model, $config, false); } @@ -124,7 +150,7 @@ class TranslateBehavior extends ModelBehavior { if (is_string($query['fields']) && $query['fields'] === "COUNT(*) AS {$db->name('count')}") { $query['fields'] = "COUNT(DISTINCT({$db->name($Model->escapeField())})) {$db->alias}count"; $query['joins'][] = array( - 'type' => 'INNER', + 'type' => $this->runtime[$Model->alias]['joinType'], 'alias' => $RuntimeModel->alias, 'table' => $joinTable, 'conditions' => array( @@ -254,7 +280,7 @@ class TranslateBehavior extends ModelBehavior { $query['fields'][] = $aliasVirtual; } $query['joins'][] = array( - 'type' => 'INNER', + 'type' => $this->runtime[$Model->alias]['joinType'], 'alias' => $alias, 'table' => $joinTable, 'conditions' => array( diff --git a/lib/Cake/Test/Case/Model/Behavior/TranslateBehaviorTest.php b/lib/Cake/Test/Case/Model/Behavior/TranslateBehaviorTest.php index 6cc10bbeb..80a9c5a45 100644 --- a/lib/Cake/Test/Case/Model/Behavior/TranslateBehaviorTest.php +++ b/lib/Cake/Test/Case/Model/Behavior/TranslateBehaviorTest.php @@ -528,6 +528,29 @@ class TranslateBehaviorTest extends CakeTestCase { $this->assertEquals($expected, $result); } + public function testMissingTranslationLeftJoin() { + $this->loadFixtures('Translate', 'TranslatedItem'); + $expected = array( + 'TranslatedItem' => Array ( + 'id' => '1', + 'translated_article_id' => '1', + 'slug' => 'first_translated', + 'locale' => 'rus', + 'content' => '', + 'title' => '', + ), + ); + + $TestModel = new TranslatedItemLeftJoin(); + $TestModel->locale = 'rus'; + $result = $TestModel->read(null, 1); + $this->assertEquals($expected, $result); + + $TestModel->locale = array('rus'); + $result = $TestModel->read(null, 1); + $this->assertEquals($expected, $result); + } + /** * testTranslatedFindList method * diff --git a/lib/Cake/Test/Case/Model/models.php b/lib/Cake/Test/Case/Model/models.php index 0a99f3ee3..babc5e7ff 100644 --- a/lib/Cake/Test/Case/Model/models.php +++ b/lib/Cake/Test/Case/Model/models.php @@ -3247,6 +3247,17 @@ class TranslatedItem extends CakeTestModel { } +class TranslatedItemLeftJoin extends TranslatedItem { + + public $actsAs = array( + 'Translate' => array( + 'content', + 'title', + 'joinType' => 'LEFT', + ) + ); +} + /** * TranslatedItem class. *