Skip to content

Commit

Permalink
This fixes the ResultSet trying to hydrate a result twice.
Browse files Browse the repository at this point in the history
Still results are being nested incorrectly, though
  • Loading branch information
lorenzo committed Apr 3, 2014
1 parent d330732 commit 6eb0a15
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 14 deletions.
1 change: 1 addition & 0 deletions src/ORM/Association.php
Expand Up @@ -14,6 +14,7 @@
*/
namespace Cake\ORM;

use Cake\Database\Expression\IdentifierExpression;
use Cake\Datasource\ResultSetDecorator;
use Cake\Event\Event;
use Cake\ORM\Entity;
Expand Down
7 changes: 0 additions & 7 deletions src/ORM/Association/BelongsToMany.php
Expand Up @@ -49,13 +49,6 @@ class BelongsToMany extends Association {
*/
const SAVE_REPLACE = 'replace';

/**
* Whether this association can be expressed directly in a query join
*
* @var boolean
*/
protected $_canBeJoined = false;

/**
* The type of join to be used when adding the association to a query
*
Expand Down
1 change: 0 additions & 1 deletion src/ORM/Association/ExternalAssociationTrait.php
Expand Up @@ -14,7 +14,6 @@
*/
namespace Cake\ORM\Association;

use Cake\Database\Expression\IdentifierExpression;
use Cake\ORM\Association\SelectableAssociationTrait;
use Cake\ORM\Query;
use Cake\Utility\Inflector;
Expand Down
1 change: 0 additions & 1 deletion src/ORM/Association/HasOne.php
Expand Up @@ -14,7 +14,6 @@
*/
namespace Cake\ORM\Association;

use Cake\Database\Expression\IdentifierExpression;
use Cake\ORM\Association;
use Cake\ORM\Association\DependentDeleteTrait;
use Cake\ORM\Association\SelectableAssociationTrait;
Expand Down
1 change: 0 additions & 1 deletion src/ORM/Association/SelectableAssociationTrait.php
Expand Up @@ -79,7 +79,6 @@ protected function _buildQuery($options) {
$target = $this->target();
$alias = $target->alias();
$key = $this->_linkField($options);

$filter = $options['keys'];

if ($options['strategy'] === $this::STRATEGY_SUBQUERY) {
Expand Down
14 changes: 14 additions & 0 deletions src/ORM/EagerLoader.php
Expand Up @@ -67,6 +67,13 @@ class EagerLoader {
*/
protected $_loadExternal = [];

/**
* Contains a list of the association names that are to be eagerly loaded
*
* @var array
*/
protected $_aliasList = [];

/**
* Sets the list of associations that should be eagerly loaded along for a
* specific table using when a query is provided. The list of associated tables
Expand Down Expand Up @@ -318,7 +325,14 @@ protected function _normalizeContain(Table $parent, $alias, $options, $paths) {
'aliasPath' => trim($paths['aliasPath'], '.'),
'propertyPath' => trim($paths['propertyPath'], '.'),
];

$config['canBeJoined'] = $instance->canBeJoined($config['config']);
if ($config['canBeJoined'] && !empty($this->_aliasList[$alias])) {
$config['canBeJoined'] = false;
$config['config']['strategy'] = $instance::STRATEGY_SELECT;
}

$this->_aliasList[$alias][] = $paths['aliasPath'];

foreach ($extra as $t => $assoc) {
$config['associations'][$t] = $this->_normalizeContain($table, $t, $assoc, $paths);
Expand Down
15 changes: 11 additions & 4 deletions tests/TestCase/ORM/QueryRegressionTest.php
Expand Up @@ -67,13 +67,20 @@ public function testEagerLoadingFromEmptyResults() {
}

public function testDuplicateAttachableAliases() {
TableRegistry::get('Stuff', ['table' => 'tags']);
TableRegistry::get('Things', ['table' => 'articles_tags']);

$table = TableRegistry::get('Articles');
$table->belongsTo('Authors');
$table->hasOne('ArticlesTags');
$table->Authors->target()->hasOne('Tags', ['foreignKey' => 'id']);
$table->ArticlesTags->target()->belongsTo('Tags', ['foreignKey' => 'tag_id']);
$table->hasOne('Things');

$table->Authors->target()->hasOne('Stuff', ['foreignKey' => 'id']);
$table->Things->target()->belongsTo('Stuff', ['foreignKey' => 'tag_id']);

$results = $table->find()->contain(['Authors.Tags', 'ArticlesTags.Tags']);
$results = $table->find()
->contain(['Authors.Stuff', 'Things.Stuff'])
->hydrate(false)
->toArray();
}

}

0 comments on commit 6eb0a15

Please sign in to comment.