Skip to content

Commit

Permalink
Final changes to make after find logic work on joinable associations,
Browse files Browse the repository at this point in the history
adding test to TranslateBehavior to show how this is possible
  • Loading branch information
lorenzo committed Feb 9, 2014
1 parent 150880f commit 510232d
Show file tree
Hide file tree
Showing 6 changed files with 132 additions and 1 deletion.
23 changes: 23 additions & 0 deletions src/Collection/CollectionTrait.php
Expand Up @@ -22,6 +22,7 @@
use Cake\Collection\Iterator\MapReduce;
use Cake\Collection\Iterator\ReplaceIterator;
use Cake\Collection\Iterator\SortIterator;
use Cake\Collection\Iterator\SyncIterator;
use LimitIterator;

/**
Expand Down Expand Up @@ -750,6 +751,28 @@ public function nest($idPath, $parentPath) {
return new Collection($collection);
}

public function insert($path, $values) {
$iterator = new SyncIterator($this, new Collection($values));
$collection = new Collection($iterator);
$path = explode('.', $path);
$target = array_pop($path);

return $collection->map(function($values) use ($path, $target) {
list($row, $insert) = $values;
$pointer =& $row;

foreach ($path as $step) {
if (!isset($pointer[$step])) {
return $row;
}
$pointer =& $pointer[$step];
}

$pointer[$target] = $insert;
return $row;
});
}

/**
* Returns an array representation of the results
*
Expand Down
34 changes: 34 additions & 0 deletions src/Collection/Iterator/SyncIterator.php
@@ -0,0 +1,34 @@
<?php
/**
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://cakephp.org CakePHP(tm) Project
* @since CakePHP(tm) v 3.0.0
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
namespace Cake\Collection\Iterator;

use Iterator;
use MultipleIterator;

class SyncIterator extends MultipleIterator {

public function __construct(Iterator $left, Iterator $right) {
$flags = static::MIT_NEED_ANY|static::MIT_KEYS_NUMERIC;
parent::__construct($flags);
$this->attachIterator($left);
$this->attachIterator($right);
}

public function key() {
$key = parent::key();
return $key[0];
}

}
24 changes: 24 additions & 0 deletions src/ORM/Association.php
Expand Up @@ -491,6 +491,30 @@ protected function _copyAttributes($query, $surrogate, $options) {
if (!empty($options['fields'])) {
$query->select($query->aliasFields($options['fields'], $target->alias()));
}

foreach ($surrogate->formatResults() as $callable) {
$query->formatResults(function($results) use ($callable) {
$property = $this->property();
$extracted = $callable($results->extract($property))->compile();
return $results->insert($property, $extracted);
});
}

$this->_bindNewAssociations($query, $surrogate, $options);
}

protected function _bindNewAssociations($query, $surrogate, $options) {
$contain = $surrogate->contain();
$target = $this->_targetTable;
if ($contain) {
$loader = $surrogate->eagerLoader();
$loader->attachAssociations($query, $target, $options['includeFields']);
$newBinds = [];
foreach ($contain as $alias => $value) {
$newBinds[$options['path'] . '.' . $alias] = $value;
}
$query->contain($newBinds);
}
}

/**
Expand Down
5 changes: 4 additions & 1 deletion src/ORM/EagerLoader.php
Expand Up @@ -251,7 +251,10 @@ public function attachAssociations(Query $query, Table $repository, $includeFiel
}

foreach ($this->attachableAssociations($repository) as $options) {
$config = $options['config'] + ['includeFields' => $includeFields];
$config = $options['config'] + [
'path' => $options['path'],
'includeFields' => $includeFields
];
$options['instance']->attachTo($query, $config);
}
}
Expand Down
1 change: 1 addition & 0 deletions tests/Fixture/TranslateFixture.php
Expand Up @@ -75,5 +75,6 @@ class TranslateFixture extends TestFixture {
array('locale' => 'eng', 'model' => 'Comments', 'foreign_key' => 3, 'field' => 'comment', 'content' => 'Comment #3'),
array('locale' => 'eng', 'model' => 'Comments', 'foreign_key' => 4, 'field' => 'comment', 'content' => 'Comment #4'),
array('locale' => 'spa', 'model' => 'Comments', 'foreign_key' => 4, 'field' => 'comment', 'content' => 'Comentario #4'),
array('locale' => 'eng', 'model' => 'Authors', 'foreign_key' => 1, 'field' => 'name', 'content' => 'May-rianoh')
);
}
46 changes: 46 additions & 0 deletions tests/TestCase/Model/Behavior/TranslateBehaviorTest.php
Expand Up @@ -410,4 +410,50 @@ public function testTranslationsHasManyWithOverride() {
$this->assertEquals('Obsah #1', $results->first()->body);
}

/**
* Tests that it is possible to translate belongsTo associations
*
* @return void
*/
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()
->select(['title', 'body'])
->contain(['Authors' => function($q) {
return $q->select(['id', 'name']);
}]);

$expected = [
[
'title' => 'Title #1',
'body' => 'Content #1',
'author' => ['id' => 1, 'name' => 'May-rianoh', '_locale' => 'eng'],
'_locale' => 'eng'
],
[
'title' => 'Title #2',
'body' => 'Content #2',
'author' => ['id' => 3, 'name' => 'larry', '_locale' => 'eng'],
'_locale' => 'eng'
],
[
'title' => 'Title #3',
'body' => 'Content #3',
'author' => ['id' => 1, 'name' => 'May-rianoh', '_locale' => 'eng'],
'_locale' => 'eng'
]
];
$results = array_map(function($r) {
return $r->toArray();
}, $results->toArray());
$this->assertEquals($expected, $results);
}

}

0 comments on commit 510232d

Please sign in to comment.