Skip to content

Commit

Permalink
Cleaning up and fixing tests
Browse files Browse the repository at this point in the history
  • Loading branch information
lorenzo committed Aug 6, 2016
1 parent 78a2306 commit 6b984fb
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 19 deletions.
29 changes: 16 additions & 13 deletions src/ORM/Association/BelongsToMany.php
Expand Up @@ -450,7 +450,7 @@ protected function _buildResultMap($fetchQuery, $options)
if (!isset($result[$this->_junctionProperty])) {
throw new RuntimeException(sprintf(
'"%s" is missing from the belongsToMany results. Results cannot be created.',
$property
$this->_junctionProperty
));
}

Expand Down Expand Up @@ -945,7 +945,7 @@ public function find($type = null, array $options = [])
* @param string|array $conditions The query conditions to use.
* @return \Cake\ORM\Query The modified query.
*/
protected function _appendJunctionJoin($query, $conditions, $options = [])
protected function _appendJunctionJoin($query, $conditions)
{
$name = $this->_junctionAssociationName();
$joins = $query->join();
Expand All @@ -962,9 +962,6 @@ protected function _appendJunctionJoin($query, $conditions, $options = [])
->addDefaultTypes($assoc->target())
->join($matching + $joins, [], true);

$targetProperty = empty($options['targetProperty']) ? null : $options['targetProperty'];
$query->eagerLoader()->addToJoinsMap($this->_name . '_Cjoin', $assoc, false, $targetProperty);

return $query;
}

Expand Down Expand Up @@ -1251,7 +1248,7 @@ protected function _buildQuery($options)
$query = $queryBuilder($query);
}

$query = $this->_appendJunctionJoin($query, [], ['targetProperty' => $this->_junctionProperty]);
$query = $this->_appendJunctionJoin($query, []);

if ($query->autoFields() === null) {
$query->autoFields($query->clause('select') === []);
Expand All @@ -1260,18 +1257,24 @@ protected function _buildQuery($options)
// Ensure that association conditions are applied
// and that the required keys are in the selected columns.

$tempName = $this->name();
$fields = $query->autoFields() ? $assoc->schema()->columns() : (array)$assoc->foreignKey();
$joinFields = [];
foreach ($fields as $f ) {
$joinFields[$tempName . '_Cjoin__' . $f] = "$name.$f";
$tempName = $this->_name . '_CJoin';
$schema = $assoc->schema();
$joinFields = $types = [];

foreach ($schema->typeMap() as $f => $type ) {
$key = $tempName . '__' . $f;
$joinFields[$key] = "$name.$f";
$types[$key] = $type;
}

$fields = array_combine(array_keys($joinFields), $fields);
$query
->where($this->junctionConditions())
->select($joinFields);
->select($joinFields)
->defaultTypes($types);

$query
->eagerLoader()
->addToJoinsMap($tempName, $assoc, false, $this->_junctionProperty);
$assoc->attachTo($query, ['aliasPath' => $assoc->alias(), 'includeFields' => false]);
return $query;
}
Expand Down
7 changes: 4 additions & 3 deletions tests/Fixture/SpecialTagsFixture.php
Expand Up @@ -34,6 +34,7 @@ class SpecialTagsFixture extends TestFixture
'tag_id' => ['type' => 'integer', 'null' => false],
'highlighted' => ['type' => 'boolean', 'null' => true],
'highlighted_time' => ['type' => 'timestamp', 'null' => true],
'extra_info' => ['type' => 'string'],
'author_id' => ['type' => 'integer', 'null' => true],
'_constraints' => [
'primary' => ['type' => 'primary', 'columns' => ['id']],
Expand All @@ -47,8 +48,8 @@ class SpecialTagsFixture extends TestFixture
* @var array
*/
public $records = [
['article_id' => 1, 'tag_id' => 3, 'highlighted' => false, 'highlighted_time' => null, 'author_id' => 1],
['article_id' => 2, 'tag_id' => 1, 'highlighted' => true, 'highlighted_time' => '2014-06-01 10:10:00', 'author_id' => 2],
['article_id' => 10, 'tag_id' => 10, 'highlighted' => true, 'highlighted_time' => '2014-06-01 10:10:00', 'author_id' => null]
['article_id' => 1, 'tag_id' => 3, 'highlighted' => false, 'highlighted_time' => null, 'extra_info' => 'Foo', 'author_id' => 1],
['article_id' => 2, 'tag_id' => 1, 'highlighted' => true, 'highlighted_time' => '2014-06-01 10:10:00', 'extra_info' => 'Bar', 'author_id' => 2],
['article_id' => 10, 'tag_id' => 10, 'highlighted' => true, 'highlighted_time' => '2014-06-01 10:10:00', 'extra_info' => 'Baz', 'author_id' => null]
];
}
2 changes: 1 addition & 1 deletion tests/Fixture/TranslatesFixture.php
Expand Up @@ -80,6 +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'],
['locale' => 'eng', 'model' => 'SpecialTags', 'foreign_key' => 2, 'field' => 'extra_info', 'content' => 'Translated Info'],
];
}
4 changes: 2 additions & 2 deletions tests/TestCase/ORM/Behavior/TranslateBehaviorTest.php
Expand Up @@ -672,7 +672,7 @@ public function testFindSingleLocaleBelongsToMany()
{
$table = TableRegistry::get('Articles');
$specialTags = TableRegistry::get('SpecialTags');
$specialTags->addBehavior('Translate', ['fields' => ['highlighted_time']]);
$specialTags->addBehavior('Translate', ['fields' => ['extra_info']]);

$table->belongsToMany('Tags', [
'through' => $specialTags
Expand All @@ -682,7 +682,7 @@ public function testFindSingleLocaleBelongsToMany()
$result = $table->get(2, ['contain' => 'Tags']);
$this->assertNotEmpty($result);
$this->assertNotEmpty($result->tags);
$this->assertEquals('Translated Time', $result->tags[0]->_joinData->highlighted_time);
$this->assertEquals('Translated Info', $result->tags[0]->special_tags[0]->extra_info);
}

/**
Expand Down

0 comments on commit 6b984fb

Please sign in to comment.