From 6eb0a15d537043e5dcf16106653f0f8274444a0b Mon Sep 17 00:00:00 2001 From: Jose Lorenzo Rodriguez Date: Thu, 3 Apr 2014 09:25:02 +0200 Subject: [PATCH] This fixes the ResultSet trying to hydrate a result twice. Still results are being nested incorrectly, though --- src/ORM/Association.php | 1 + src/ORM/Association/BelongsToMany.php | 7 ------- src/ORM/Association/ExternalAssociationTrait.php | 1 - src/ORM/Association/HasOne.php | 1 - .../Association/SelectableAssociationTrait.php | 1 - src/ORM/EagerLoader.php | 14 ++++++++++++++ tests/TestCase/ORM/QueryRegressionTest.php | 15 +++++++++++---- 7 files changed, 26 insertions(+), 14 deletions(-) diff --git a/src/ORM/Association.php b/src/ORM/Association.php index e83ce1c4c3f..7e8edf12130 100644 --- a/src/ORM/Association.php +++ b/src/ORM/Association.php @@ -14,6 +14,7 @@ */ namespace Cake\ORM; +use Cake\Database\Expression\IdentifierExpression; use Cake\Datasource\ResultSetDecorator; use Cake\Event\Event; use Cake\ORM\Entity; diff --git a/src/ORM/Association/BelongsToMany.php b/src/ORM/Association/BelongsToMany.php index 62cea3b9a72..9ca279d77c5 100644 --- a/src/ORM/Association/BelongsToMany.php +++ b/src/ORM/Association/BelongsToMany.php @@ -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 * diff --git a/src/ORM/Association/ExternalAssociationTrait.php b/src/ORM/Association/ExternalAssociationTrait.php index 9e7b5f79c8d..0ece0efa1e8 100644 --- a/src/ORM/Association/ExternalAssociationTrait.php +++ b/src/ORM/Association/ExternalAssociationTrait.php @@ -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; diff --git a/src/ORM/Association/HasOne.php b/src/ORM/Association/HasOne.php index d3183da58e0..1ad0ea147c5 100644 --- a/src/ORM/Association/HasOne.php +++ b/src/ORM/Association/HasOne.php @@ -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; diff --git a/src/ORM/Association/SelectableAssociationTrait.php b/src/ORM/Association/SelectableAssociationTrait.php index 1674e5ff9f8..c8a9e89d889 100644 --- a/src/ORM/Association/SelectableAssociationTrait.php +++ b/src/ORM/Association/SelectableAssociationTrait.php @@ -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) { diff --git a/src/ORM/EagerLoader.php b/src/ORM/EagerLoader.php index ef29a5261a5..9d2046adc77 100644 --- a/src/ORM/EagerLoader.php +++ b/src/ORM/EagerLoader.php @@ -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 @@ -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); diff --git a/tests/TestCase/ORM/QueryRegressionTest.php b/tests/TestCase/ORM/QueryRegressionTest.php index f6367317e87..81fbd367cb7 100644 --- a/tests/TestCase/ORM/QueryRegressionTest.php +++ b/tests/TestCase/ORM/QueryRegressionTest.php @@ -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(); } }