diff --git a/RedBean/SQLHelper.php b/RedBean/SQLHelper.php index 17a29577a..ef9e0cd80 100644 --- a/RedBean/SQLHelper.php +++ b/RedBean/SQLHelper.php @@ -289,4 +289,19 @@ public function getNew() { return new self( $this->adapter ); } + + /** + * When cast to string, simply print the query and its bindings. + * + * @return string + */ + public function __toString() + { + list( $query, $params ) = $this->getQuery(); + + return print_r( array( + 'query' => $query, + 'params' => $params + ), true ); + } } diff --git a/testing/RedUNIT/Mysql/Mix.php b/testing/RedUNIT/Mysql/Mix.php index 6056c1704..b91165ea3 100644 --- a/testing/RedUNIT/Mysql/Mix.php +++ b/testing/RedUNIT/Mysql/Mix.php @@ -116,6 +116,26 @@ public function testQueryBuilderMix() asrt( ( $value == 2 ), TRUE ); } + + /** + * Test the __toString method of the SQLHelper. + */ + public function testToString() + { + $toolbox = R::$toolbox; + $adapter = $toolbox->getDatabaseAdapter(); + $sqlHelper = new RedBean_SQLHelper( $adapter ); + $sqlHelper->begin() + ->select( '*' ) + ->from( 'table' ) + ->where( 'name = ?' ) + ->put( 'name' ); + $str = (string) $sqlHelper; + asrt( ( strpos( $str, 'query' ) !== false ), true ); + asrt( ( strpos( $str, 'select * from table where name = ?' ) !== false ), true ); + asrt( ( strpos( $str, '=> Array') !== false ), true ); + asrt( ( strpos( $str, 'params') !== false ), true ); + } }