CakePHP 2.x CookBook Chinese Version Chapter 7 model data deletion

Source: Internet
Author: User

The model class for deleting data CakePHP provides several methods to delete records from the database. Deletedelete (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. DeleteAlldeleteAll (mixed $ conditions, $ cascade = true, $ callbacks = false) Apart from deleteAll (), it will delete all records that match the provided condition, and delete () basically the same. The $ conditions array is provided as an SQL segment or array. Set the logical value of the condition cascade to true. When a condition is deleted, the dependency record is deleted. Callbacks logic value. If the callback is successful, True is returned. If the callback fails, False is returned. Example: 1 // The condition used for deleting is the same as find (). 2 $ this-> Comment-> deleteAll (array ('comment. spam '=> true), false); If a row with callback and/or cascade is deleted, the row is searched and deleted. This usually results in multiple queries. Note: If the deletion condition is successfully executed but no matching record exists, deleteAll () returns true even if no record is deleted.

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.