Jump to:

3460 Posts in 1064 Topics by 739 members

Data Model Questions

SilverStripe Forums » Data Model Questions » how to automatically delete objects when page is deleted?

Moderators: martimiz, Sean, biapar, Willr, Ingo, swaiba, simon_w

Page: 1
Go to End
Author Topic: 952 Views
  • lise
    Avatar
    Community Member
    47 Posts

    how to automatically delete objects when page is deleted? Link to this post

    Hello,

    let's imagine I have a page defined as follows :

    class MyPage extends Page {
        static $db = array(

       etc..

    );

    static $has_many = array(
             'myEntry' => 'myEntry'
       );

    }

    'myEntry' is simply an object and is in a relation *only* with myPage. These objects are managed with the DOM.

    When a page of type 'myPage' is deleted, the associated 'myObjects' objects remain in the database. Because the
    application can only have one myPage , I would like that when a page 'myPage' is deleted, all the 'myEntries' be deleted too.

    What is the best way to achieve this? Can we attached a function to the myPage class which will be called when an instance is
    deleted from the Published site ?

    An acceptable workaround would be to remind the administrator to remove object entries before he/she attempts to delete a page
    but this would also require to 'catch' the action in CMS and attach a sort of callback function.

    Please let me know if you have any tips or ideas ...or if this does not make much sense ..

    Thanks,

    Lise

  • Willr
    Avatar
    Forum Moderator
    5508 Posts

    Re: how to automatically delete objects when page is deleted? Link to this post

    You could use the built in onBeforeDelete() or onAfterDelete() handlers on your MyPage class

    <?php

    class MyPage extends Page {
    //...

    function onBeforeDelete() {

    parent::onBeforeDelete();

    // .. delete your objects

    }

    952 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.