Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

Data Model Questions /

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

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


Go to End


5 Posts   7577 Views

Avatar
joern

Community Member, 28 Posts

23 August 2010 at 4:02am

Edited: 23/08/2010 4:35am

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

Avatar
Willr

Forum Moderator, 5523 Posts

23 August 2010 at 9:14am

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

Avatar
joern

Community Member, 28 Posts

23 August 2010 at 9:49pm

Edited: 23/08/2010 9:50pm

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

Avatar
EzraNaj

Community Member, 11 Posts

10 September 2010 at 12:48am

I encountered this problem too. Can somebody help?

Avatar
Willr

Forum Moderator, 5523 Posts

10 September 2010 at 4:12pm

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'");