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.

Data Model Questions /

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


Reply


3 Posts   384 Views

Avatar
Optic Blaze

Community Member, 165 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, 5513 Posts

4 October 2013 at 8:01pm

setDelete?

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

Avatar
Optic Blaze

Community Member, 165 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();
   }