Skip to content

Commit

Permalink
Prevent unnecessary joins / complex conditions in delete
Browse files Browse the repository at this point in the history
  • Loading branch information
mvdriel committed Jul 25, 2016
1 parent b50c064 commit d6e4513
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/Cake/Model/Datasource/Database/Mysql.php
Expand Up @@ -439,7 +439,7 @@ public function delete(Model $model, $conditions = null) {
}
$complexConditions = false;
foreach ((array)$conditions as $key => $value) {
if (strpos($key, $model->alias) === false) {
if (strpos($key, '.') !== false && strpos($key, $model->alias) === false) {
$complexConditions = true;
break;
}
Expand Down
21 changes: 21 additions & 0 deletions lib/Cake/Test/Case/Model/Datasource/Database/MysqlTest.php
Expand Up @@ -4045,6 +4045,27 @@ public function testDeleteStatements() {
$this->Dbo->delete($Article, '2=2');
}

/**
* Test deletes without complex conditions.
*
* @return void
*/
public function testDeleteNoComplexCondition() {
$this->loadFixtures('Article', 'User');
$test = ConnectionManager::getDatasource('test');
$db = $test->config['database'];

$this->Dbo = $this->getMock('Mysql', array('execute'), array($test->config));

$this->Dbo->expects($this->at(0))->method('execute')
->with("DELETE `Article` FROM `$db`.`articles` AS `Article` WHERE `id` = 1");

$Article = new Article();

$conditions = array('id' => 1);
$this->Dbo->delete($Article, $conditions);
}

/**
* Test truncate with a mock.
*
Expand Down

0 comments on commit d6e4513

Please sign in to comment.