Search code examples
databasecakephpsql-delete

unnecessary guard on CakePHP deleteall


I've taken over a CakePHP 2.9 code-base and am puzzled by code of the form:

$chkFeed = $this->UserFeed->find('count', array('conditions' => array('UserFeed.user_id' => $userID)));
if ($chkFeed > 0) {
    $this->UserFeed->deleteAll(array('UserFeed.user_id' => $userID));
}

surely that if ... guard is unnecessary? You can just call deleteAll even if in fact no rows are deleted, right? This says so, so why have the guard?


Solution

  • "If" guard is unnecessary here .

    Following code will also do your job perfectly . As deleteAll() works both way match or not match. In both cases it will return true.

    $chkFeed = $this->UserFeed->find('count', array('conditions' => array('UserFeed.user_id' => $userID)));
    $this->UserFeed->deleteAll(array('UserFeed.user_id' => $userID));