LogoLogo
HomeAPIBlog
3.0.0-SNAPSHOT
3.0.0-SNAPSHOT
  • INTRODUCTION
    • Getting Started
      • Running Local Development Servers
      • Beginner Tutorial: Hello World
      • Beginner Tutorial: Hello Database
      • Tutorial: Wheels, AJAX, and You
    • Frameworks and Wheels
    • Requirements
    • Manual Installation
    • Upgrading
    • Screencasts
  • Command Line Tools
    • CLI Commands
    • wheels - commands
    • wheels generate - commands
    • wheels dbmigrate - commands
    • wheels plugins - commands
  • Working with Wheels
    • Conventions
    • Configuration and Defaults
    • Directory Structure
    • Switching Environments
    • Testing Your Application
    • Contributing to Wheels
    • Documenting your Code
  • Handling Requests with Controllers
    • Request Handling
    • Rendering Content
    • Redirecting Users
    • Sending Files
    • Sending Email
    • Responding with Multiple Formats
    • Using the Flash
    • Using Filters
    • Verification
    • Event Handlers
    • Routing
    • URL Rewriting
      • Apache
      • IIS
      • Tomcat
      • Nginx
    • Obfuscating URLs
    • Caching
    • Nesting Controllers
    • CORS Requests
  • Displaying Views to Users
    • Pages
    • Partials
    • Linking Pages
    • Layouts
    • Form Helpers and Showing Errors
    • Displaying Links for Pagination
    • Date, Media, and Text Helpers
    • Creating Custom View Helpers
    • Localization
  • Database Interaction Through Models
    • Object Relational Mapping
    • Creating Records
    • Reading Records
    • Updating Records
    • Deleting Records
    • Column Statistics
    • Dynamic Finders
    • Getting Paginated Data
    • Associations
    • Nested Properties
    • Object Validation
    • Object Callbacks
    • Calculated Properties
    • Transactions
    • Dirty Records
    • Soft Delete
    • Automatic Time Stamps
    • Database Migrations
      • Migrations in Production
    • Using Multiple Data Sources
  • Plugins
    • Installing and Using Plugins
    • Developing Plugins
    • Publishing Plugins
  • Project Documentation
    • Overview
  • External Links
    • Source Code
    • Issue Tracker
    • Sponsor Us
    • Community
Powered by GitBook
LogoLogo
On this page
  • Delete Callbacks
  • Example of Deleting a Record

Was this helpful?

Edit on GitHub
Export as PDF
  1. Database Interaction Through Models

Deleting Records

Deleting records from your database tables.

PreviousUpdating RecordsNextColumn Statistics

Last updated 21 days ago

Was this helpful?

Deleting records in Wheels is simple. If you have fetched an object, you can just call its method. If you don't have any callbacks specified for the class, all that will happen is that the record will be deleted from the table and true will be returned.

Delete Callbacks

If you have callbacks however, this is what happens:

First, all methods registered to be run before a delete happens (these are registered using a call from the config function) will be executed, if any exist.

If these return true, Wheels will proceed and delete the record from the table. If false is returned from the callback code, processing will return to your code without the record being deleted. (false is returned to you in this case.)

If the record was deleted, the callback code is executed, and whatever that code returns will be returned to you. (You should make all your callbacks return true or false.)

If you're unfamiliar with the concept of callbacks, you can read about them in the chapter.

Example of Deleting a Record

Here's a simple example of fetching a record from the database and then deleting it.

aPost = model("post").findByKey(33);
aPost.delete();

There are also 3 class-level delete methods available: , , and . These work similarly to the class level methods for updating, which you can read more about in .

delete()
beforeDelete()
afterDelete()
Object Callbacks
deleteByKey()
deleteOne()
deleteAll()
Updating Records