From e4fbba3221f1ea8770a9c58b2ed50a56e4a37084 Mon Sep 17 00:00:00 2001 From: Jose Lorenzo Rodriguez Date: Wed, 12 Jun 2013 00:00:06 +0200 Subject: [PATCH] Some tests where not actually running, specifically for the subquery strategry, making them run and fixing broken tests --- lib/Cake/Database/Expression/Comparison.php | 2 +- lib/Cake/ORM/Association.php | 7 +++---- lib/Cake/ORM/Association/BelongsTo.php | 2 +- .../Association/ExternalAssociationTrait.php | 1 + lib/Cake/ORM/Query.php | 5 +++-- lib/Cake/Test/TestCase/ORM/QueryTest.php | 18 +++++++++--------- 6 files changed, 18 insertions(+), 17 deletions(-) diff --git a/lib/Cake/Database/Expression/Comparison.php b/lib/Cake/Database/Expression/Comparison.php index f2dad0f82e2..4f15035c14d 100644 --- a/lib/Cake/Database/Expression/Comparison.php +++ b/lib/Cake/Database/Expression/Comparison.php @@ -1,6 +1,6 @@ diff --git a/lib/Cake/ORM/Association.php b/lib/Cake/ORM/Association.php index f40681ca2ff..587f19ca886 100644 --- a/lib/Cake/ORM/Association.php +++ b/lib/Cake/ORM/Association.php @@ -369,7 +369,7 @@ public function attachTo(Query $query, array $options = []) { } $joinOptions = ['table' => 1, 'conditions' => 1, 'type' => 1]; - $query->join([$target->alias() => array_intersect_key($options, $joinOptions)]); + $query->join([$this->_name => array_intersect_key($options, $joinOptions)]); if (empty($options['fields'])) { $f = isset($options['fields']) ? $options['fields'] : null; @@ -379,7 +379,7 @@ public function attachTo(Query $query, array $options = []) { } if (!empty($options['fields'])) { - $query->select($query->aliasFields($options['fields'], $target->alias())); + $query->select($query->aliasFields($options['fields'], $this->_name)); } } @@ -392,8 +392,7 @@ public function attachTo(Query $query, array $options = []) { */ public function transformRow($row) { $sourceAlias = $this->source()->alias(); - $targetAlias = $this->target()->alias(); - $row[$sourceAlias][$this->property()] = $row[$targetAlias]; + $row[$sourceAlias][$this->property()] = $row[$this->_name]; return $row; } diff --git a/lib/Cake/ORM/Association/BelongsTo.php b/lib/Cake/ORM/Association/BelongsTo.php index 6da9c1e3d44..eaf095d7170 100644 --- a/lib/Cake/ORM/Association/BelongsTo.php +++ b/lib/Cake/ORM/Association/BelongsTo.php @@ -61,7 +61,7 @@ public function foreignKey($key = null) { */ protected function _joinCondition(array $options) { return sprintf('%s.%s = %s.%s', - $this->target()->alias(), + $this->_name, $this->_targetTable->primaryKey(), $this->_sourceTable->alias(), $options['foreignKey'] diff --git a/lib/Cake/ORM/Association/ExternalAssociationTrait.php b/lib/Cake/ORM/Association/ExternalAssociationTrait.php index b32ffd90af9..2b1308f4d38 100644 --- a/lib/Cake/ORM/Association/ExternalAssociationTrait.php +++ b/lib/Cake/ORM/Association/ExternalAssociationTrait.php @@ -238,6 +238,7 @@ protected function _buildSubquery($query, $foreignKey) { unset($joins[$i]); } } + $filterQuery->join($joins, [], true); return $filterQuery->select($foreignKey, true); } diff --git a/lib/Cake/ORM/Query.php b/lib/Cake/ORM/Query.php index b67f759d9da..65ba05205ba 100644 --- a/lib/Cake/ORM/Query.php +++ b/lib/Cake/ORM/Query.php @@ -412,8 +412,9 @@ protected function _transformQuery() { if (!$this->_dirty) { return parent::_transformQuery(); } - - $this->from([$this->_table->alias() => $this->_table->table()]); + if (empty($this->_parts['from'])) { + $this->from([$this->_table->alias() => $this->_table->table()]); + } $this->_addDefaultFields(); $this->_addContainments(); return parent::_transformQuery(); diff --git a/lib/Cake/Test/TestCase/ORM/QueryTest.php b/lib/Cake/Test/TestCase/ORM/QueryTest.php index d7866b9559e..3c15aa4b606 100644 --- a/lib/Cake/Test/TestCase/ORM/QueryTest.php +++ b/lib/Cake/Test/TestCase/ORM/QueryTest.php @@ -76,19 +76,19 @@ public function tearDown() { * @return void */ protected function _createTables() { - $table = 'CREATE TEMPORARY TABLE authors(id int, name varchar(50))'; + $table = 'CREATE TABLE authors(id int, name varchar(50))'; $this->connection->execute($table); - $table = 'CREATE TEMPORARY TABLE articles(id int, title varchar(20), body varchar(50), author_id int)'; + $table = 'CREATE TABLE articles(id int, title varchar(20), body varchar(50), author_id int)'; $this->connection->execute($table); - $table = 'CREATE TEMPORARY TABLE publications(id int, title varchar(20), body varchar(50), author_id int)'; + $table = 'CREATE TABLE publications(id int, title varchar(20), body varchar(50), author_id int)'; $this->connection->execute($table); - $table = 'CREATE TEMPORARY TABLE tags(id int, name varchar(20))'; + $table = 'CREATE TABLE tags(id int, name varchar(20))'; $this->connection->execute($table); - $table = 'CREATE TEMPORARY TABLE articles_tags(article_id int, tag_id int)'; + $table = 'CREATE TABLE articles_tags(article_id int, tag_id int)'; $this->connection->execute($table); Table::config('authors', ['connection' => $this->connection]); @@ -362,7 +362,7 @@ public function testContainResultFetchingOneLevel() { * @return void */ public function strategiesProvider() { - return [['subquery', 'select']]; + return [['subquery'], ['select']]; } /** @@ -463,7 +463,7 @@ public function testHasManyEagerLoadingFields($strategy) { * @dataProvider strategiesProvider * @return void **/ - public function testHasManyEagerLoadingOrder() { + public function testHasManyEagerLoadingOrder($strategy) { $statement = $this->_insertRecords(); $statement->bindValue(1, 3, 'integer'); $statement->bindValue(2, 'a fine title'); @@ -511,7 +511,7 @@ public function testHasManyEagerLoadingOrder() { * @dataProvider strategiesProvider * @return void **/ - public function testHasManyEagerLoadingDeep() { + public function testHasManyEagerLoadingDeep($strategy) { $this->_insertRecords(); $query = new Query($this->connection); @@ -558,7 +558,7 @@ public function testHasManyEagerLoadingDeep() { * @dataProvider strategiesProvider * @return void **/ - public function testHasManyEagerLoadingFromSecondaryTable() { + public function testHasManyEagerLoadingFromSecondaryTable($strategy) { $this->_insertRecords(); $query = new Query($this->connection);