Skip to content

Instantly share code, notes, and snippets.

@markstory
Created July 30, 2016 18:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save markstory/89cb562f311be4e545c0a3fe817170f3 to your computer and use it in GitHub Desktop.
Save markstory/89cb562f311be4e545c0a3fe817170f3 to your computer and use it in GitHub Desktop.
diff --git a/src/ORM/Association.php b/src/ORM/Association.php
index b863ed8..38ddc7a 100644
--- a/src/ORM/Association.php
+++ b/src/ORM/Association.php
@@ -839,8 +839,9 @@ abstract class Association
}
$newContain = [];
+ $aliasPrefix = isset($options['aliasPath']) ? $options['aliasPath'] . '.' : '';
foreach ($contain as $alias => $value) {
- $newContain[$options['aliasPath'] . '.' . $alias] = $value;
+ $newContain[$aliasPrefix . $alias] = $value;
}
$eagerLoader = $query->eagerLoader();
@@ -848,7 +849,7 @@ abstract class Association
foreach ($matching as $alias => $value) {
$eagerLoader->matching(
- $options['aliasPath'] . '.' . $alias,
+ $aliasPrefix . $alias,
$value['queryBuilder'],
$value
);
diff --git a/src/ORM/Association/BelongsToMany.php b/src/ORM/Association/BelongsToMany.php
index 89a2ae7..6a1540c 100644
--- a/src/ORM/Association/BelongsToMany.php
+++ b/src/ORM/Association/BelongsToMany.php
@@ -1245,7 +1245,14 @@ class BelongsToMany extends Association
->where($this->junctionConditions())
->select($query->aliasFields((array)$assoc->foreignKey(), $name));
- $assoc->attachTo($query);
+ // TODO this hasMany results in a list of records when the additional
+ // hasOne association is spliced in by TranslateBehavior.
+ // That hasOne association might be causing the _joinData to end up as a list
+ // as the associations are attached differently.
+ //
+ // The added aliasPath fixes warnings, but doesn't fix the actual issue.
+ $assoc->attachTo($query, ['aliasPath' => $assoc->alias()]);
+ debug($query);
return $query;
}
diff --git a/tests/Fixture/TranslatesFixture.php b/tests/Fixture/TranslatesFixture.php
index b8bdd8f..ea6e344 100644
--- a/tests/Fixture/TranslatesFixture.php
+++ b/tests/Fixture/TranslatesFixture.php
@@ -80,5 +80,6 @@ class TranslatesFixture extends TestFixture
['locale' => 'eng', 'model' => 'Authors', 'foreign_key' => 1, 'field' => 'name', 'content' => 'May-rianoh'],
['locale' => 'dan', 'model' => 'NumberTrees', 'foreign_key' => 1, 'field' => 'name', 'content' => 'Elektroniker'],
['locale' => 'dan', 'model' => 'NumberTrees', 'foreign_key' => 11, 'field' => 'name', 'content' => 'Alien Tingerne'],
+ ['locale' => 'eng', 'model' => 'SpecialTags', 'foreign_key' => 2, 'field' => 'highlighted_time', 'content' => 'Translated Time'],
];
}
diff --git a/tests/TestCase/ORM/Behavior/TranslateBehaviorTest.php b/tests/TestCase/ORM/Behavior/TranslateBehaviorTest.php
index 6aab8e4..cf46107 100644
--- a/tests/TestCase/ORM/Behavior/TranslateBehaviorTest.php
+++ b/tests/TestCase/ORM/Behavior/TranslateBehaviorTest.php
@@ -44,6 +44,8 @@ class TranslateBehaviorTest extends TestCase
public $fixtures = [
'core.articles',
'core.authors',
+ 'core.special_tags',
+ 'core.tags',
'core.comments',
'core.translates'
];
@@ -661,6 +663,28 @@ class TranslateBehaviorTest extends TestCase
$this->assertEquals($expected, $results);
}
+ /**
+ * Tests that it is possible to translate belongsToMany associations
+ *
+ * @return void
+ */
+ public function testFindSingleLocaleBelongsToMany()
+ {
+ $table = TableRegistry::get('Articles');
+ $specialTags = TableRegistry::get('SpecialTags');
+ $specialTags->addBehavior('Translate', ['fields' => ['highlighted_time']]);
+
+ $table->belongsToMany('Tags', [
+ 'through' => $specialTags
+ ]);
+ $specialTags->locale('eng');
+
+ $result = $table->get(2, ['contain' => 'Tags']);
+ $this->assertNotEmpty($result);
+ $this->assertNotEmpty($result->tags);
+ $this->assertEquals('Translated Time', $result->tags[0]->_joinData->highlighted_time);
+ }
+
/**
* Tests that updating an existing record translations work
*
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment