From e6295f1eb6d11c077cbbe5e9d942b7d4047b1218 Mon Sep 17 00:00:00 2001 From: mark_story Date: Sat, 22 Jun 2013 22:27:50 -0400 Subject: [PATCH] Add method to execute an ORM\Query and get a statement back. It is sometimes useful to have access to the statement object. In the case of updateAll/deleteAll, the row count is useful. Shoehorning the rowcount into ORM\ResultSet felt more awkward. --- lib/Cake/Database/Query.php | 2 +- lib/Cake/ORM/Query.php | 12 +++++++++++- lib/Cake/ORM/Table.php | 12 ++++++------ lib/Cake/Test/TestCase/ORM/TableTest.php | 8 ++++---- 4 files changed, 22 insertions(+), 12 deletions(-) diff --git a/lib/Cake/Database/Query.php b/lib/Cake/Database/Query.php index feafbd424f2..28f4698b93e 100644 --- a/lib/Cake/Database/Query.php +++ b/lib/Cake/Database/Query.php @@ -180,7 +180,7 @@ public function connection($connection = null) { * Resulting statement is traversable, so it can be used in any loop as you would * with an array. * - * @return Cake\Database\Statement + * @return Cake\Database\StatementInterface */ public function execute() { $query = $this->_transformQuery(); diff --git a/lib/Cake/ORM/Query.php b/lib/Cake/ORM/Query.php index 2489b50d03e..2e74553abb3 100644 --- a/lib/Cake/ORM/Query.php +++ b/lib/Cake/ORM/Query.php @@ -307,7 +307,7 @@ public function normalizedContainments() { /** * Compiles the SQL representation of this query and executes it using the - * configured connection object. Returns a ResultSet iterator object + * provided connection object. Returns a ResultSet iterator object * * Resulting object is traversable, so it can be used in any loop as you would * with an array. @@ -318,6 +318,16 @@ public function execute() { return new ResultSet($this, parent::execute()); } +/** + * Compiles the SQL representation of this query ane executes it using + * the provided connection object. + * + * @return Cake\Database\StatementInterface + */ + public function executeStatement() { + return parent::execute(); + } + /** * Returns an array representation of the results after executing the query. * diff --git a/lib/Cake/ORM/Table.php b/lib/Cake/ORM/Table.php index 29e481332ce..8459de9ad15 100644 --- a/lib/Cake/ORM/Table.php +++ b/lib/Cake/ORM/Table.php @@ -495,15 +495,15 @@ protected function _buildQuery() { * * @param array $fields A hash of field => new value. * @param array $conditions An array of conditions, similar to those used with find() - * @return boolean Success + * @return boolean Success Returns true if one or more rows are effected. */ public function updateAll($fields, $conditions) { $query = $this->_buildQuery(); $query->update($this->table()) ->set($fields) ->where($conditions); - $query->execute(); - return true; + $statement = $query->executeStatement(); + return $statement->rowCount() > 0; } /** @@ -519,14 +519,14 @@ public function updateAll($fields, $conditions) { * with this method. * * @param array $conditions An array of conditions, similar to those used with find() - * @return boolean Success + * @return boolean Success Returns true if one or more rows are effected. */ public function deleteAll($conditions) { $query = $this->_buildQuery(); $query->delete($this->table()) ->where($conditions); - $query->execute(); - return true; + $statement = $query->executeStatement(); + return $statement->rowCount() > 0; } } diff --git a/lib/Cake/Test/TestCase/ORM/TableTest.php b/lib/Cake/Test/TestCase/ORM/TableTest.php index 13195d37d98..e273c1a5f17 100644 --- a/lib/Cake/Test/TestCase/ORM/TableTest.php +++ b/lib/Cake/Test/TestCase/ORM/TableTest.php @@ -402,12 +402,12 @@ public function testUpdateAllFailure() { ['_buildQuery'], ['table' => 'users', 'connection' => $this->connection] ); - $query = $this->getMock('Cake\ORM\Query', ['execute'], [$this->connection]); + $query = $this->getMock('Cake\ORM\Query', ['executeStatement'], [$this->connection]); $table->expects($this->once()) ->method('_buildQuery') ->will($this->returnValue($query)); $query->expects($this->once()) - ->method('execute') + ->method('executeStatement') ->will($this->throwException(new \Cake\Database\Exception('Not good'))); $table->updateAll(['username' => 'mark'], []); } @@ -438,12 +438,12 @@ public function testDeleteAllFailure() { ['_buildQuery'], ['table' => 'users', 'connection' => $this->connection] ); - $query = $this->getMock('Cake\ORM\Query', ['execute'], [$this->connection]); + $query = $this->getMock('Cake\ORM\Query', ['executeStatement'], [$this->connection]); $table->expects($this->once()) ->method('_buildQuery') ->will($this->returnValue($query)); $query->expects($this->once()) - ->method('execute') + ->method('executeStatement') ->will($this->throwException(new \Cake\Database\Exception('Not good'))); $table->deleteAll(['id >' => 4]); }