CakePHP2.xCookBook Chinese version Chapter 7 model data deletion. The model class for deleting data CakePHP provides several methods to delete records from the database. Deletedelete (int $ idnull, boolean $ cascadetrue); delete a record with $ id. Data is deleted by default.
The CakePHP model class provides several methods to delete records from the database.
Delete
Delete (int $ id = null, boolean $ cascade = true );
Delete a record with $ id. By default, records that depend on deleted records are also deleted.
For example, to delete a User record related to many Recipe records (User 'hasbucket' or 'hasandbelongstomany 'Recipes ):
If $ cascade is set to true, Related Recipe records are also deleted (the dependent-value of the model is set to true ).
If $ cascade is set to false, the Recipe record is retained after the User is deleted.
If the database supports foreign keys and cascading deletion, it will be more dependent on CakePHP cascade. One of the benefits of using Model: delete () cascade features is that it allows you to use behavior and Model callbacks:
1 $ this-> Comment-> delete ($ this-> request-> data ('comment. id '));
You can use beforeDelete and afterDelete that both exist in the model and behavior to mount custom logic to the delete process. For more information, see callback methods.
DeleteAll
DeleteAll (mixed $ conditions, $ cascade = true, $ callbacks = false)
Except that deleteAll () will delete all records that match the provided conditions, it is basically the same as delete. The $ conditions array is provided as an SQL segment or array.
Condition to be matched
Set the logic value of cascade to true. when deleted, dependency records are also deleted.
Callbacks logic value, run callback
True is returned when the call succeeds, and False is returned when the call fails.
Example:
1 // The deletion conditions are the same as find ().
2 $ this-> Comment-> deleteAll (array ('comment. spam' => true), false );
If a row with a callback and/or cascade is deleted, the row is queried and deleted. This usually results in multiple queries.
Annotation
If the deletion condition is successfully executed but no matching record exists, deleteAll () returns true even if no record is deleted.
The CakePHP model class provides several methods to delete records from the database. Delete (int $ id = null, boolean $ cascade = true); delete a record with $ id. By default...