Skip to content

Commit 534040f

Browse files
committed
Implemented Connection::delete()
1 parent a2f3744 commit 534040f

File tree

2 files changed

+47
-2
lines changed

2 files changed

+47
-2
lines changed

lib/Cake/Model/Datasource/Database/Connection.php

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -190,10 +190,23 @@ public function update($table, array $data, array $conditions = array(), $types
190190
*
191191
* @param string $table the table to delete rows from
192192
* @param array $conditions conditions to be set for delete statement
193+
* @param array $types list of associative array containing the types to be used for casting
193194
* @return Cake\Model\Datasource\Database\Statement
194195
**/
195-
public function delete($table, $conditions = array()) {
196-
196+
public function delete($table, $conditions = array(), $types = array()) {
197+
$this->connect();
198+
$conditionsKeys = array_keys($conditions);
199+
$sql = 'DELETE FROM %s %s';
200+
list($conditions, $params) = $this->_parseConditions($conditions);
201+
$sql = sprintf(
202+
$sql,
203+
$table,
204+
$conditions
205+
);
206+
if (!empty($types)) {
207+
$types = $this->_mapTypes($conditionsKeys, $types);
208+
}
209+
return $this->execute($sql, $params, $types);
197210
}
198211

199212
/**

lib/Cake/Test/TestCase/Model/Datasource/Database/ConnectionTest.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -333,4 +333,36 @@ public function testUpdateWithConditionsAndTypes() {
333333
$this->assertEquals('2012-01-01', $row['body']);
334334
}
335335

336+
/**
337+
* Tests delete from table with no conditions
338+
*
339+
* @return void
340+
**/
341+
public function testDeleteNoConditions() {
342+
$this->_insertTwoRecords();
343+
$this->connection->delete('things');
344+
$result = $this->connection->execute('SELECT * FROM things');
345+
$this->assertCount(0, $result);
346+
}
347+
348+
349+
/**
350+
* Tests delete from table with conditions
351+
* @return void
352+
**/
353+
public function testDeleteWithConditions() {
354+
$this->_insertTwoRecords();
355+
$this->connection->delete('things', array('id' => '1-rest-is-ommited'), array('id' => 'integer'));
356+
$result = $this->connection->execute('SELECT * FROM things');
357+
$this->assertCount(1, $result);
358+
359+
$this->connection->delete('things', array('id' => '1-rest-is-ommited'), array('id' => 'integer'));
360+
$result = $this->connection->execute('SELECT * FROM things');
361+
$this->assertCount(1, $result);
362+
363+
$this->connection->delete('things', array('id' => '2-rest-is-ommited'), array('id' => 'integer'));
364+
$result = $this->connection->execute('SELECT * FROM things');
365+
$this->assertCount(0, $result);
366+
}
367+
336368
}

0 commit comments

Comments
 (0)