Skip to content
Permalink
Browse files

First steps towards supporting translatable belongsTo associations

  • Loading branch information...
lorenzo committed Jan 26, 2014
1 parent 2a9c9ff commit 7d397bc0ba4d2712253f4cb6ebec0f803b2b0aac
@@ -89,7 +89,7 @@ public function __construct(Table $table, array $config = []) {
public function setupFieldAssociations($fields) {
$alias = $this->_table->alias();
foreach ($fields as $field) {
$name = $field . '_translation';
$name = $this->_table->alias() . '_' . $field . '_translation';
$target = TableRegistry::get($name);
$target->table('i18n');
@@ -137,8 +137,9 @@ public function beforeFind(Event $event, $query) {
$contain = [];
$fields = $this->config()['fields'];
$alias = $this->_table->alias();
foreach ($fields as $field) {
$contain[$field . '_translation'] = $conditions;
$contain[$alias . '_' . $field . '_translation'] = $conditions;
}
$query->contain($contain);
@@ -412,7 +412,7 @@ public function attachTo(Query $query, array $options = []) {
if (!empty($options['queryBuilder'])) {
$newQuery = $options['queryBuilder']($target->query());
$options['fields'] = $newQuery->clause('select') ?: $options['fields'];
$options['conditions']->add($newQuery->clause('where'));
$options['conditions']->add($newQuery->clause('where') ?: []);
}
$joinOptions = ['table' => 1, 'conditions' => 1, 'type' => 1];
@@ -34,7 +34,8 @@ class TranslateBehaviorTest extends TestCase {
public $fixtures = [
'core.translate',
'core.article',
'core.comment'
'core.comment',
'core.author'
];
public function tearDown() {
@@ -393,4 +394,20 @@ public function testTranslationsHasManyWithOverride() {
$this->assertEquals('Obsah #1', $results->first()->body);
}
public function testFindSingleLocaleBelongsto() {
$table = TableRegistry::get('Articles');
$table->addBehavior('Translate', ['fields' => ['title', 'body']]);
$authors = $table->belongsTo('Authors')->target();
$authors->addBehavior('Translate', ['fields' => ['name']]);
$table->locale('eng');
$authors->locale('eng');
$results = $table->find()->contain(['Authors' => function($q) {
return $q->select(['id', 'name']);
}]);
debug(json_encode($results->first()->author));
}
}

0 comments on commit 7d397bc

Please sign in to comment.
You can’t perform that action at this time.