mirror of
https://github.com/kamilwylegala/cakephp2-php8.git
synced 2024-11-15 03:18:26 +00:00
Merge pull request #10764 from bancer/translate-inner-joins
Add left join support in TranslateBehavior
This commit is contained in:
commit
8cd930d19e
3 changed files with 63 additions and 3 deletions
|
@ -60,6 +60,25 @@ class TranslateBehavior extends ModelBehavior {
|
||||||
* $config could be empty - and translations configured dynamically by
|
* $config could be empty - and translations configured dynamically by
|
||||||
* bindTranslation() method
|
* 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 Model $Model Model the behavior is being attached to.
|
||||||
* @param array $config Array of configuration information.
|
* @param array $config Array of configuration information.
|
||||||
* @return mixed
|
* @return mixed
|
||||||
|
@ -75,7 +94,14 @@ class TranslateBehavior extends ModelBehavior {
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->settings[$Model->alias] = array();
|
$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);
|
$this->translateModel($Model);
|
||||||
return $this->bindTranslation($Model, $config, false);
|
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')}") {
|
if (is_string($query['fields']) && $query['fields'] === "COUNT(*) AS {$db->name('count')}") {
|
||||||
$query['fields'] = "COUNT(DISTINCT({$db->name($Model->escapeField())})) {$db->alias}count";
|
$query['fields'] = "COUNT(DISTINCT({$db->name($Model->escapeField())})) {$db->alias}count";
|
||||||
$query['joins'][] = array(
|
$query['joins'][] = array(
|
||||||
'type' => 'INNER',
|
'type' => $this->runtime[$Model->alias]['joinType'],
|
||||||
'alias' => $RuntimeModel->alias,
|
'alias' => $RuntimeModel->alias,
|
||||||
'table' => $joinTable,
|
'table' => $joinTable,
|
||||||
'conditions' => array(
|
'conditions' => array(
|
||||||
|
@ -254,7 +280,7 @@ class TranslateBehavior extends ModelBehavior {
|
||||||
$query['fields'][] = $aliasVirtual;
|
$query['fields'][] = $aliasVirtual;
|
||||||
}
|
}
|
||||||
$query['joins'][] = array(
|
$query['joins'][] = array(
|
||||||
'type' => 'INNER',
|
'type' => $this->runtime[$Model->alias]['joinType'],
|
||||||
'alias' => $alias,
|
'alias' => $alias,
|
||||||
'table' => $joinTable,
|
'table' => $joinTable,
|
||||||
'conditions' => array(
|
'conditions' => array(
|
||||||
|
|
|
@ -528,6 +528,29 @@ class TranslateBehaviorTest extends CakeTestCase {
|
||||||
$this->assertEquals($expected, $result);
|
$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
|
* testTranslatedFindList method
|
||||||
*
|
*
|
||||||
|
|
|
@ -3247,6 +3247,17 @@ class TranslatedItem extends CakeTestModel {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
class TranslatedItemLeftJoin extends TranslatedItem {
|
||||||
|
|
||||||
|
public $actsAs = array(
|
||||||
|
'Translate' => array(
|
||||||
|
'content',
|
||||||
|
'title',
|
||||||
|
'joinType' => 'LEFT',
|
||||||
|
)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* TranslatedItem class.
|
* TranslatedItem class.
|
||||||
*
|
*
|
||||||
|
|
Loading…
Reference in a new issue