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
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');
	}
Example #2
0
 /**
  * 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");
     $this->Dbo->expects($this->at(1))->method('execute')->with("DELETE `Article` FROM `{$db}`.`articles` AS `Article`   WHERE NOT (`id` = 1)");
     $Article = new Article();
     $conditions = array('id' => 1);
     $this->Dbo->delete($Article, $conditions);
     $conditions = array('NOT' => array('id' => 1));
     $this->Dbo->delete($Article, $conditions);
 }
Example #3
0
 /**
  * Test deletes with a mock.
  *
  * @return void
  */
 public function testDeleteStatements()
 {
     $this->loadFixtures('Boat', '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}`.`boats`  WHERE 1 = 1");
     $this->Dbo->expects($this->at(1))->method('execute')->with("DELETE `Boat` FROM `{$db}`.`boats` AS `Boat` LEFT JOIN `{$db}`.`users` AS `User` " . "ON (`Boat`.`user_id` = `User`.`id`)" . "  WHERE 1 = 1");
     $this->Dbo->expects($this->at(2))->method('execute')->with("DELETE `Boat` FROM `{$db}`.`boats` AS `Boat` LEFT JOIN `{$db}`.`users` AS `User` " . "ON (`Boat`.`user_id` = `User`.`id`)" . "  WHERE 2=2");
     $Boat = new Boat();
     $this->Dbo->delete($Boat);
     $this->Dbo->delete($Boat, true);
     $this->Dbo->delete($Boat, '2=2');
 }
Example #4
0
 /**
 	 408:  * Generates and executes an SQL DELETE statement for given id/conditions on given model.
 	 409:  *
 	 410:  * @param Model $model
 	 411:  * @param mixed $conditions
 	 412:  * @return boolean Success
 	 413:  */
 public function delete(Model $model, $conditions = null)
 {
     if (!$this->_useAlias) {
         return parent::delete($model, $conditions);
     }
     $alias = $this->name($model->alias);
     $table = $this->fullTableName($model);
     $joins = implode(' ', $this->_getJoins($model));
     if (empty($conditions)) {
         $alias = $joins = false;
     }
     $complexConditions = false;
     foreach ((array) $conditions as $key => $value) {
         if (strpos($key, $model->alias) === false) {
             $complexConditions = true;
             break;
         }
     }
     if (!$complexConditions) {
         $joins = false;
     }
     $conditions = $this->conditions($this->defaultConditions($model, $conditions, $alias), true, true, $model);
     if ($conditions === false) {
         return false;
     }
     if ($this->execute($this->renderStatement('delete', compact('alias', 'table', 'joins', 'conditions'))) === false) {
         $model->onError();
         return false;
     }
     return true;
 }
Example #5
0
 /**
  * Generates and executes an SQL DELETE statement for given id/conditions on given model.
  *
  * @param Model $model
  * @param mixed $conditions
  * @return boolean Success
  */
 function delete(&$model, $conditions = null)
 {
     if (!$this->_useAlias) {
         return parent::delete($model, $conditions);
     }
     $alias = $this->name($model->alias);
     $table = $this->fullTableName($model);
     $joins = implode(' ', $this->_getJoins($model));
     if (empty($conditions)) {
         $alias = $joins = false;
     }
     $conditions = $this->conditions($this->defaultConditions($model, $conditions, $alias), true, true, $model);
     if ($conditions === false) {
         return false;
     }
     if ($this->execute($this->renderStatement('delete', compact('alias', 'table', 'joins', 'conditions'))) === false) {
         $model->onError();
         return false;
     }
     return true;
 }