diff --git a/src/Database/Query.php b/src/Database/Query.php index a3359c2be92..4200a4ce534 100644 --- a/src/Database/Query.php +++ b/src/Database/Query.php @@ -204,7 +204,7 @@ public function execute() { $query->_bindStatement($statement); $statement->execute(); - return $query->_decorateStatement($statement); + return $this->_iterator = $query->_decorateStatement($statement); } /** @@ -1821,4 +1821,20 @@ public function __toString() { return sprintf('(%s)', $this->sql()); } +/** + * Returns an array that can be used to describe the internal state of this + * object. + * + * @return array + */ + public function __debugInfo() { + return [ + 'sql' => $this->sql(), + 'params' => $this->valueBinder()->bindings(), + 'defaultTypes' => $this->_defaultTypes, + 'decorators' => count($this->_resultDecorators), + 'executed' => $this->_iterator ? true : false + ]; + } + } diff --git a/src/ORM/Query.php b/src/ORM/Query.php index 6a3c07d6766..18a45e06424 100644 --- a/src/ORM/Query.php +++ b/src/ORM/Query.php @@ -708,4 +708,19 @@ public function __call($method, $arguments) { ); } +/** + * {@inheritdoc} + */ + public function __debugInfo() { + return parent::__debugInfo() + [ + 'hydrate' => $this->_hydrate, + 'buffered' => $this->_useBufferedResults, + 'formatters' => count($this->_formatters), + 'mapReducers' => count($this->_mapReduce), + 'contain' => $this->contain(), + 'extraOptions' => $this->_options, + 'repository' => $this->_repository + ]; + } + } diff --git a/src/ORM/ResultSet.php b/src/ORM/ResultSet.php index ea3658b5b6e..c39c759f113 100644 --- a/src/ORM/ResultSet.php +++ b/src/ORM/ResultSet.php @@ -439,4 +439,17 @@ protected function _bufferResult($result) { } } +/** + * Returns an array that can be used to describe the internal state of this + * object. + * + * @return array + */ + public function __debugInfo() { + return [ + 'query' => $this->_query, + 'items' => $this->toArray(), + ]; + } + } diff --git a/tests/TestCase/Database/QueryTest.php b/tests/TestCase/Database/QueryTest.php index cc3acb8ad3e..9901810cbba 100644 --- a/tests/TestCase/Database/QueryTest.php +++ b/tests/TestCase/Database/QueryTest.php @@ -2396,6 +2396,42 @@ public function testQuotingInsert() { $this->assertQuotedQuery('INSERT INTO \(\(bar\)\)', $sql); } +/** + * Tests __debugInfo + * + * @return void + */ + public function testDebugInfo() { + $query = (new Query($this->connection))->select('*') + ->from('articles') + ->defaultTypes(['id' => 'integer']) + ->where(['id' => '1']); + + $expected = [ + 'sql' => $query->sql(), + 'params' => [ + ':c0' => ['value' => '1', 'type' => 'integer', 'placeholder' => 'c0'] + ], + 'defaultTypes' => ['id' => 'integer'], + 'decorators' => 0, + 'executed' => false + ]; + $result = $query->__debugInfo(); + $this->assertEquals($expected, $result); + + $query->execute(); + $expected = [ + 'sql' => $query->sql(), + 'params' => [ + ':c0' => ['value' => '1', 'type' => 'integer', 'placeholder' => 'c0'] + ], + 'defaultTypes' => ['id' => 'integer'], + 'decorators' => 0, + 'executed' => true + ]; + $result = $query->__debugInfo(); + } + /** * Assertion for comparing a table's contents with what is in it. * diff --git a/tests/TestCase/ORM/QueryTest.php b/tests/TestCase/ORM/QueryTest.php index a27fff4c4a4..442ffe78403 100644 --- a/tests/TestCase/ORM/QueryTest.php +++ b/tests/TestCase/ORM/QueryTest.php @@ -1767,4 +1767,52 @@ public function testContainInAssociationMatching() { $this->assertEquals('tag3', $results[0]->articles->articles_tags->tag->name); } +/** + * Tests __debugInfo + * + * @return void + */ + public function testDebugInfo() { + $table = TableRegistry::get('authors'); + $table->hasMany('articles'); + $query = $table->find() + ->where(['id > ' => 1]) + ->bufferResults(false) + ->hydrate(false) + ->matching('articles') + ->applyOptions(['foo' => 'bar']) + ->formatResults(function($results) { + return $results; + }) + ->mapReduce(function($item, $key, $mr) { + $mr->emit($item); + }); + + $expected = [ + 'sql' => $query->sql(), + 'params' => $query->valueBinder()->bindings(), + 'defaultTypes' => [ + 'authors.id' => 'integer', + 'id' => 'integer', + 'authors.name' => 'string', + 'name' => 'string' + ], + 'decorators' => 0, + 'executed' => false, + 'hydrate' => false, + 'buffered' => false, + 'formatters' => 1, + 'mapReducers' => 1, + 'contain' => [ + 'articles' => [ + 'queryBuilder' => null, + 'matching' => true + ] + ], + 'extraOptions' => ['foo' => 'bar'], + 'repository' => $table + ]; + $this->assertSame($expected, $query->__debugInfo()); + } + } diff --git a/tests/TestCase/ORM/ResultSetTest.php b/tests/TestCase/ORM/ResultSetTest.php index b6f961765d3..3527c07192d 100644 --- a/tests/TestCase/ORM/ResultSetTest.php +++ b/tests/TestCase/ORM/ResultSetTest.php @@ -230,4 +230,19 @@ public function testGroupBy() { $this->assertEquals($expected, $results); } +/** + * Tests __debugInfo + * + * @return void + */ + public function testDebugInfo() { + $query = $this->table->find('all'); + $results = $query->all(); + $expected = [ + 'query' => $query, + 'items' => $results->toArray() + ]; + $this->assertSame($expected, $results->__debugInfo()); + } + }