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

[SOLVED] How to delete item from DataObject via front end


Go to End


3 Posts   1345 Views

Avatar
Optic Blaze

Community Member, 190 Posts

2 October 2013 at 10:44am

Hi there,

I am trying my hand at some front end forms and i have created a dataobject called Customers.
I can list my customers with the following code in the front end:

// Display list of customers
function ShowCustomers() {
return Customer::get();
}

But how do i delete a customer? At the moment i have this:

// Delete customer
function DeleteCustomer() {
$SQL_id = (int) $this->ID;
echo $SQL_id;
$page = DataObject::get_by_id('Customer', $SQL_id);
$page->setDelete(true);
Controller::curr()->redirectBack();
}

Thanks

Avatar
Willr

Forum Moderator, 5523 Posts

4 October 2013 at 8:01pm

setDelete?

To delete an object simply called delete. $obj->delete()

Avatar
Optic Blaze

Community Member, 190 Posts

4 October 2013 at 9:24pm

Thanks Willr,

I did the following and it worked for deleting a record in the customer.php dataobject
-------------------------------------------------------------------------------------------------------------
//Set permission to allow deletion function below
public static $allowed_actions = array ('delete');

//Delete customer when the following is typed in URL: customer/delete/10 (the record to delete)
public function delete ($request) {
$param = $request->allParams();
$delparam = $param["ID"];
Customer::get()->byID($delparam)->delete();
Controller::curr()->redirectBack();
}