diff --git a/src/ORM/Query.php b/src/ORM/Query.php index e3b339ab0d8..21415d19537 100644 --- a/src/ORM/Query.php +++ b/src/ORM/Query.php @@ -1306,9 +1306,9 @@ public function insert(array $columns, array $types = []) * @param \Cake\ORM\Table $table The table this query is starting on * @return static */ - public function subquery(Table $table) + public static function subquery(Table $table) { - $query = new static($this->getConnection(), $table); + $query = new static($table->getConnection(), $table); $query->aliasingEnabled = false; return $query; diff --git a/tests/TestCase/ORM/QueryTest.php b/tests/TestCase/ORM/QueryTest.php index 5311f9a580e..f3a9c491394 100644 --- a/tests/TestCase/ORM/QueryTest.php +++ b/tests/TestCase/ORM/QueryTest.php @@ -4009,8 +4009,8 @@ public function testWith(): void */ public function testSubqueryConnection() { - $subquery = (new Query($this->connection, $this->table))->subquery($this->table); - $this->assertEquals($this->connection, $subquery->getConnection()); + $subquery = Query::subquery($this->table); + $this->assertEquals($this->table->getConnection(), $subquery->getConnection()); } /** @@ -4021,7 +4021,7 @@ public function testSubqueryConnection() public function testSubqueryAliasing() { $articles = $this->getTableLocator()->get('Articles'); - $subquery = (new Query($this->connection, $this->table))->subquery($articles); + $subquery = Query::subquery($articles); $subquery->select('Articles.field1'); $this->assertRegExpSql(