CakePHP2.xCookBook Chinese version Chapter 7 model data deletion _ PHP Tutorial-php Tutorial

Source: Internet
Author: User
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...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.