Merge pull request #10764 from bancer/translate-inner-joins

Add left join support in TranslateBehavior
This commit is contained in:
Mark Story 2017-06-26 21:40:42 -04:00 committed by GitHub
commit 8cd930d19e
3 changed files with 63 additions and 3 deletions

View file

@ -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(

View file

@ -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
*

View file

@ -3247,6 +3247,17 @@ class TranslatedItem extends CakeTestModel {
}
class TranslatedItemLeftJoin extends TranslatedItem {
public $actsAs = array(
'Translate' => array(
'content',
'title',
'joinType' => 'LEFT',
)
);
}
/**
* TranslatedItem class.
*