DboSource::delete PHP Method

delete() public method

For databases that do not support aliases in UPDATE queries.
public delete ( Model $Model, mixed $conditions = null ) : boolean
$Model Model The model to delete from
$conditions mixed The conditions to use. If empty the model's primary key will be used.
return boolean Success
    public function delete(Model $Model, $conditions = null)
    {
        $alias = $joins = null;
        $table = $this->fullTableName($Model);
        $conditions = $this->_matchRecords($Model, $conditions);
        if ($conditions === false) {
            return false;
        }
        if ($this->execute($this->renderStatement('delete', compact('alias', 'table', 'joins', 'conditions'))) === false) {
            $Model->onError();
            return false;
        }
        return true;
    }

Usage Example

Example #1
0
/**
 * Test deletes with a mock.
 *
 * @return void
 */
	public function testDeleteStatements() {
		$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  FROM `$db`.`articles`  WHERE 1 = 1");

		$this->Dbo->expects($this->at(1))->method('execute')
			->with("DELETE `Article` FROM `$db`.`articles` AS `Article` LEFT JOIN `$db`.`users` AS `User` " .
				"ON (`Article`.`user_id` = `User`.`id`)" .
				"  WHERE 1 = 1");

		$this->Dbo->expects($this->at(2))->method('execute')
			->with("DELETE `Article` FROM `$db`.`articles` AS `Article` LEFT JOIN `$db`.`users` AS `User` " .
				"ON (`Article`.`user_id` = `User`.`id`)" .
				"  WHERE 2=2");
		$Article = new Article();

		$this->Dbo->delete($Article);
		$this->Dbo->delete($Article, true);
		$this->Dbo->delete($Article, '2=2');
	}
All Usage Examples Of DboSource::delete
DboSource