Skip to content

Commit

Permalink
Merge pull request #1369 from markstory/3.0-table-delete
Browse files Browse the repository at this point in the history
3.0 - Add basic implementation of deleteAll().
  • Loading branch information
lorenzo committed Jun 23, 2013
2 parents 21d0236 + e6295f1 commit bec7cbf
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 7 deletions.
2 changes: 1 addition & 1 deletion lib/Cake/Database/Query.php
Expand Up @@ -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();
Expand Down
12 changes: 11 additions & 1 deletion lib/Cake/ORM/Query.php
Expand Up @@ -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.
Expand All @@ -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.
*
Expand Down
29 changes: 26 additions & 3 deletions lib/Cake/ORM/Table.php
Expand Up @@ -495,15 +495,38 @@ 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;
}

/**
* Delete all matching rows.
*
* Deletes all rows matching the provided conditions.
*
* This method will *not* trigger beforeDelete/afterDelete events. If you
* need those first load a collection of records and delete them.
*
* This method will *not* execute on associations `cascade` attribute. You should
* use database foreign keys + ON CASCADE rules if you need cascading deletes combined
* with this method.
*
* @param array $conditions An array of conditions, similar to those used with find()
* @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);
$statement = $query->executeStatement();
return $statement->rowCount() > 0;
}

}
40 changes: 38 additions & 2 deletions lib/Cake/Test/TestCase/ORM/TableTest.php
Expand Up @@ -402,14 +402,50 @@ 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'], []);
}

/**
* Test deleting many records.
*
* @return void
*/
public function testDeleteAll() {
$table = new Table(['table' => 'users', 'connection' => $this->connection]);
$result = $table->deleteAll(['id <' => 4]);
$this->assertTrue($result);

$result = $table->find('all')->toArray();
$this->assertCount(1, $result, 'Only one record should remain');
$this->assertEquals(4, $result[0]['id']);
}

/**
* Test that exceptions from the Query bubble up.
*
* @expectedException Cake\Database\Exception
*/
public function testDeleteAllFailure() {
$table = $this->getMock(
'Cake\ORM\Table',
['_buildQuery'],
['table' => 'users', 'connection' => $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('executeStatement')
->will($this->throwException(new \Cake\Database\Exception('Not good')));
$table->deleteAll(['id >' => 4]);
}

}

0 comments on commit bec7cbf

Please sign in to comment.