Home >Backend Development >PHP Tutorial >CakePHP 2.x CookBook Chinese Version Chapter 7 Model - Deleting Data_PHP Tutorial

CakePHP 2.x CookBook Chinese Version Chapter 7 Model - Deleting Data_PHP Tutorial

WBOY
WBOYOriginal
2016-07-14 10:08:50830browse

Delete data

CakePHP’s model class provides several methods for deleting records from the database.
delete
delete(int $id = null, boolean $cascade = true);
Delete record by $id. By default, records that are dependent on the deleted record are also deleted.
For example, to delete a User record that is related to many Recipe records (User ‘hasMany’ or ‘hasAndBelongsToMany’ Recipes):
If $cascade is set to true, the related Recipe record is also deleted (the model's dependent-value is set to true).
If $cascade is set to false, the Recipe record will still be retained after the User is deleted.
If the database supports foreign keys and cascade deletion, it will be relied on before CakePHP's cascade. One of the benefits of using the cascading feature of Model:delete() is that it allows you to use behavior and model callbacks:
1 $this->Comment->delete($this->request->data('Comment.id'));
You can hook custom logic to the deletion process using beforeDelete and afterDelete that exist in both models and behaviors. See callback methods for more information.
deleteAll
deleteAll(mixed $conditions, $cascade = true, $callbacks = false)
Basically the same as delete() except that deleteAll() will delete all records that match the provided criteria. The $conditions array will be provided as a SQL fragment or array.
conditions Conditions to match
cascade logical value, set to true, when deleting, records with dependencies will be deleted at the same time.
callbacks logical value, run callback
Returns True on success and False on failure.
Example:
1 // The conditions used for deletion are the same as find()
2 $this->Comment->deleteAll(array('Comment.spam' => true), false);
If deleted with callbacks and/or cascades, the relevant rows will be found and deleted. This often results in multiple queries being issued.
Annotations
When the delete condition executes successfully but no records match, deleteAll() will return true even if no records are deleted.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/477771.htmlTechArticleDelete Data CakePHP's model class provides several methods for deleting records from the database. delete delete(int $id = null, boolean $cascade = true); Delete the record by $id. By default...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn