Skip to content

Commit

Permalink
Take table name in delete().
Browse files Browse the repository at this point in the history
This saves having to call from() as well.
  • Loading branch information
markstory committed Mar 3, 2013
1 parent 5240068 commit 7aba2d1
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 5 deletions.
6 changes: 5 additions & 1 deletion lib/Cake/Model/Datasource/Database/Query.php
Expand Up @@ -1092,11 +1092,15 @@ public function update() {
* Can be combined with from(), where() and other methods to
* create delete queries with specific conditions.
*
* @param string $table The table to use when deleting. This
* @return Query
*/
public function delete() {
public function delete($table = null) {
$this->_dirty = true;
$this->_type = 'delete';
if ($table) {
$this->from($table);
}
return $this;
}

Expand Down
27 changes: 23 additions & 4 deletions lib/Cake/Test/TestCase/Model/Datasource/Database/QueryTest.php
Expand Up @@ -1575,11 +1575,11 @@ public function testDecorateResults() {
}

/**
* Test a basic delete.
* Test a basic delete using from()
*
* @return void
*/
public function testDeleteSimple() {
public function testDeleteWithFrom() {
$this->_insertTwoRecords();
$query = new Query($this->connection);

Expand All @@ -1595,6 +1595,26 @@ public function testDeleteSimple() {
$this->assertCount(2, $result);
}

/**
* Test a basic delete with no from() call.
*
* @return void
*/
public function testDeleteNoFrom() {
$this->_insertTwoRecords();
$query = new Query($this->connection);

$query->delete('authors')
->where('1 = 1');

$result = $query->sql(false);
$this->assertContains('DELETE FROM authors ', $result);

$result = $query->execute();
$this->assertInstanceOf('Cake\Model\Datasource\Database\Statement', $result);
$this->assertCount(2, $result);
}

/**
* Test setting select() & delete() modes.
*
Expand All @@ -1604,8 +1624,7 @@ public function testSelectAndDeleteOnSameQuery() {
$this->_insertTwoRecords();
$query = new Query($this->connection);
$result = $query->select()
->delete()
->from('authors')
->delete('authors')
->where('1 = 1');
$result = $query->sql(false);

Expand Down

0 comments on commit 7aba2d1

Please sign in to comment.