Skip to content

Commit

Permalink
Implemented Connection::delete()
Browse files Browse the repository at this point in the history
  • Loading branch information
lorenzo committed Oct 14, 2012
1 parent a2f3744 commit 534040f
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 2 deletions.
17 changes: 15 additions & 2 deletions lib/Cake/Model/Datasource/Database/Connection.php
Expand Up @@ -190,10 +190,23 @@ public function update($table, array $data, array $conditions = array(), $types
*
* @param string $table the table to delete rows from
* @param array $conditions conditions to be set for delete statement
* @param array $types list of associative array containing the types to be used for casting
* @return Cake\Model\Datasource\Database\Statement
**/
public function delete($table, $conditions = array()) {

public function delete($table, $conditions = array(), $types = array()) {
$this->connect();
$conditionsKeys = array_keys($conditions);
$sql = 'DELETE FROM %s %s';
list($conditions, $params) = $this->_parseConditions($conditions);
$sql = sprintf(
$sql,
$table,
$conditions
);
if (!empty($types)) {
$types = $this->_mapTypes($conditionsKeys, $types);
}
return $this->execute($sql, $params, $types);
}

/**
Expand Down
Expand Up @@ -333,4 +333,36 @@ public function testUpdateWithConditionsAndTypes() {
$this->assertEquals('2012-01-01', $row['body']);
}

/**
* Tests delete from table with no conditions
*
* @return void
**/
public function testDeleteNoConditions() {
$this->_insertTwoRecords();
$this->connection->delete('things');
$result = $this->connection->execute('SELECT * FROM things');
$this->assertCount(0, $result);
}


/**
* Tests delete from table with conditions
* @return void
**/
public function testDeleteWithConditions() {
$this->_insertTwoRecords();
$this->connection->delete('things', array('id' => '1-rest-is-ommited'), array('id' => 'integer'));
$result = $this->connection->execute('SELECT * FROM things');
$this->assertCount(1, $result);

$this->connection->delete('things', array('id' => '1-rest-is-ommited'), array('id' => 'integer'));
$result = $this->connection->execute('SELECT * FROM things');
$this->assertCount(1, $result);

$this->connection->delete('things', array('id' => '2-rest-is-ommited'), array('id' => 'integer'));
$result = $this->connection->execute('SELECT * FROM things');
$this->assertCount(0, $result);
}

}

0 comments on commit 534040f

Please sign in to comment.