Jump to:

3375 Posts in 999 Topics by 712 members

Data Model Questions

SilverStripe Forums » Data Model Questions » "$this->delete()" won't delete Dataobject on all Stages

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

Page: 1
Go to End
Author Topic: 2949 Views
  • joern
    Avatar
    Community Member
    28 Posts

    "$this->delete()" won't delete Dataobject on all Stages Link to this post

    Hi,
    after update to 2.4.1 i can't delete Dataobjects on all Stages from my Frontend-Forms (it works in the admin). The entry is only deleted in the _Live-Table and keeps it in the _Versions-Table and Stage-Table.

    Member Decorator:

    function extraStatics() {
       return array(
          'has_many' => array( // Einladungen
             'Invitations' => 'Invitation'
          ),
          …
    }

    function removeInvitation($invitationID) {
          
       $invitationID = (int) $invitationID;
          
       // prevents access to invitations from other users
       if( !$this->owner->Invitations()->containsIDs(array($invitationID))) return false;
       
       if (!is_numeric($invitationID) || $invitationID < 1) return false;
       
       $invitation = $this->owner->Invitations()->find("ID", $invitationID);
       
       if (!$invitation) return false;
          
       // unset from member list
       $this->owner->Invitations()->remove($invitation);
       
       // delete
       $invitation->delete();
    }

    Invitation:

    class Invitation extends DataObject {

       static $extensions = array(
          "Versioned('Stage', 'Live')"
       );

       …
    }

    Do you know something?

    Best Regards ,
    Jörn

  • Willr
    Avatar
    Forum Moderator
    5464 Posts

    Re: "$this->delete()" won't delete Dataobject on all Stages Link to this post

    Probably because $invitation is a single dataobject and delete() only deletes the object in question and not any of its staged components. I think the easiest way is to call the deleteFromStage() function on the object to delete from each.

    http://api.silverstripe.org/2.4/sapphire/model/Versioned.html#methoddeleteFromStage

  • joern
    Avatar
    Community Member
    28 Posts

    Re: "$this->delete()" won't delete Dataobject on all Stages Link to this post

    Hi,
    thanks

       $invitation->deleteFromStage("Live");
       $invitation->deleteFromStage("Stage");
       $invitation->delete();

    delete it on both stages but keeps the Versions.

    i'm a little bit confused, because i looked in the docs http://api.silverstripe.org/2.4/sapphire/model/DataObject.html#methoddelete and read:

    void delete( )
    Delete this data object.
    $this->onBeforeDelete() gets called. Note that in Versioned objects, both Stage and Live will be deleted.

    i try also "DataObjcect::get("Invitation", … )->delete(); but it only removes the data from the _Live-Table.

    Something is wrong. The Delete-Method or the Docs.

    Jörn

  • EzraNaj
    Avatar
    Community Member
    11 Posts

    Re: "$this->delete()" won't delete Dataobject on all Stages Link to this post

    I encountered this problem too. Can somebody help?

  • Willr
    Avatar
    Forum Moderator
    5464 Posts

    Re: "$this->delete()" won't delete Dataobject on all Stages Link to this post

    delete it on both stages but keeps the Versions.

    Yes it won't delete versions as delete() may imply you want to revert to it (versions is like a history). Not sure if the ORM has any functionality for that (eg try allVersions()) but you could always use good old plain Database queries!

    DB::query("DELETE FROM Table_versions WHERE RecordID = '$invitation->ID'");

    2949 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.