Take the 2-minute tour ×
Craft CMS Stack Exchange is a question and answer site for administrators, end users, developers and designers for Craft CMS. It's 100% free, no registration required.

When an entry is created with my plugin's field type, data is saved to a separate table with entries id for reference. I need to delete that data if the entry that is associated with the data is deleted. How and where should I listen for entry delete event to execute a method to delete this separate data?

share|improve this question

2 Answers 2

up vote 3 down vote accepted

You can add something like this to your plugin's init() method:

craft()->on('entries.onDeleteEntry', function(Event $event) {
    // Do your custom logic here.
    // Note that you can access the entry that was just deleted via $event->params['entry']
});

Or, better yet, from your custom table, make sure the foreign key that you've created on your entryId column back to the entries table has a CASCADE DELETE setup on it so that when Craft deletes the entry, it will cascade down and delete it from your table as well.

share|improve this answer
    
Is the CASCADE DELETE added to the defineRelations return array in the Record file? –  aran Jul 8 at 4:36
1  
Yeah... check out craft/app/records for examples: 'element' => array(static::BELONGS_TO, 'ElementRecord', 'id', 'required' => true, 'onDelete' => static::CASCADE), –  Brad Bell Jul 8 at 4:38
1  
Or just see Lindsey's answer for an actual example. :) –  Brad Bell Jul 8 at 4:41
    
Sorry, I felt compelled to include a more elaborate example (and note my opinion on which method was best). But Brad's answer is definitely the right one, because it lists both ways of achieving this goal... including the event listener technique, which was originally mentioned by @aran. –  Lindsey D Jul 8 at 4:45
    
It's also worth noting that the event listener technique has some drawbacks... The example above is only listening for the deletion of an entry. And while that directly answers the original question, if your data were ever linked to an element other than an entry, that listener would be useless. –  Lindsey D Jul 8 at 4:47

Brad's answer is totally correct.

As the question was posed by the original asker, Brad provided an example of an event listener which would be triggered when an entry is deleted. He also mentions the CASCADE DELETE capabilities of a Record.

In my opinion, the absolute best way to achieve what you're doing is by properly setting up your Record. In your defineRelations method, you can create a proper relationship between your table and the main elements table...

class MyPlugin_MyRecord extends BaseRecord
{

    // ...

    public function defineRelations()
    {
        return array(
            'element' => array(static::BELONGS_TO, 'ElementRecord', 'required' => true, 'onDelete' => static::CASCADE),
        );
    }

}
share|improve this answer
    
thanks for the example. –  aran Jul 8 at 4:52
    
Also remember that a user might not want your data deleted when they delete the entry, so a database level cascade delete might not always be the best place to put the business rule. Using the event listener allows you to decide whether or not to delete the record. –  Luke Holder Jul 8 at 6:42
    
For example what if the related record is a product that has orders against it? Deleting the entry might delete the product entry but you will still want to link to the historic product from order history. In that case I would pace a field on the record called 'deleted' as boolean. Anyways point is be careful with cascade delete logic on the db layer. –  Luke Holder Jul 8 at 6:44
    
My plugin has two records both created when the entry my plugin's custom input type is associated with is saved. Both records use the entry's id as keys for their entries. Record (A) has a one to one relationship to the entry id. Record (B) has a one to many relationship with entry id. I am unclear with the information above how to setup the relation so 'onDelete' works. –  aran Jul 8 at 16:55
    
It sounds like you'd just need to define that relationship in both of your records, nearly identical to the example code. Despite the fact that they are one-to-one VS one-to-many, I think they will both use BELONGS_TO because they are both the child tables of the main elements table. Check out this page on Yii relationship terms... –  Lindsey D Jul 8 at 17:14

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.