TRIGGERS – STOPPING ACTIONS

DBObjects have several methods that will get called when the ORM is in a position to perform an action against them and returning NO from these methods will cancel the operation and rollback the save-point that had been created for it.

- (BOOL)entityWillUpdate;
- (BOOL)entityWillDelete;
- (BOOL)entityWillInsert;

These are also used to cascade changes to other classes as well. Or stop an action if there are runtime level constraints you wish to observe. Below the example looks to see if there are any related objects in another class and if so it will cancel the action.

- (BOOL)entityWillDelete {
   
   // we are about to delete a customer, check they have no orders
   if([[[Order query] whereWithFormat:@"customer = %@", self] count]) {
       // cancel the action and prevent the object from being deleted
       return NO;
   }

   return YES;

}