Jump to:

3372 Posts in 998 Topics by 712 members

Data Model Questions

SilverStripe Forums » Data Model Questions » [SOLVED] How to delete item from DataObject via front end

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

Page: 1
Go to End
Author Topic: 258 Views
  • Optic Blaze
    Avatar
    Community Member
    150 Posts

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

    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

  • Willr
    Avatar
    Forum Moderator
    5462 Posts

    Re: [SOLVED] How to delete item from DataObject via front end Link to this post

    setDelete?

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

  • Optic Blaze
    Avatar
    Community Member
    150 Posts

    Re: [SOLVED] How to delete item from DataObject via front end Link to this post

    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();
       }

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