Jump to:

3433 Posts in 1058 Topics by 734 members

Data Model Questions

SilverStripe Forums » Data Model Questions » DataObject

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

Page: 1
Go to End
Author Topic: 998 Views
  • Rhodin
    Avatar
    Community Member
    3 Posts

    DataObject Link to this post

    Hey guys, I am new to Silverstripe and am trying to make sth.
    I have made a custom class that extends DataObject (myClass), has some fields (title, a link, some text to enter)
    I have made a page that extends Page that is using DataObject::get() in function myFunction to get all the DataObjects (myClass) and template that is using <% control myFunction %> the function to loop through all of myClass and display them
    Now, I have a form for entering new myClass, but I need one to edit it.
    Any1 can help me with frontend edit form and how to call it?
    Should I make a new page that will have the form? How to call it?

  • swaiba
    Avatar
    Forum Moderator
    1786 Posts
  • Rhodin
    Avatar
    Community Member
    3 Posts

    Re: DataObject Link to this post

    Not sure that i follow what would help me there?
    I already made the DataObject with a front end form to enter a new DataObject on the page to display them.
    When I loop through them on that page i am displaying all the info (the data I fill in the form to enter a new myClass), I have also been able to create a delete function so I can delete the chosen myClass, but I haven't found a way to edit one...

    delete:

       function delmyClass($request) {
          $param = $request->allParams();
          $myClassID=(int)$param['ID'];
          DB::query("DELETE FROM myClass WHERE ID = '$myClassID'");
          Director::redirectBack();
       }

    and from template I call it with

       <% control myFunction %>   
          <li>

             ...

             <% if Top.canDelmyClass %>
                <a href="{$delURL}">Delete</a>
             <% end_if %>
             <% if Top.canEditmyClass %>
                <a href="{$EditURL}">Edit</a>
             <% end_if %>
             
          </li>

       <% end_control %>

    Now, delete function works, and I have been trying to make edit function, but whatever i do I can't seem to make it work.
    Has any1 tried this?
    Can any1 tell me where to make edit form? I have to go to another page that will have edit form and fill it with data... How to send the data from specific DataObject myClass to it?
    I don't want to have that page in my site, so I would like to do it with render with (page) or sth...
    Can any1 help me with that? How to link to the form, give needed data to it, display it with that data, edit it, save, then redirect back to the page I was on.
    Thank you all in advance!

  • swaiba
    Avatar
    Forum Moderator
    1786 Posts

    Re: DataObject Link to this post

    ok, well you asked for help on creating an edit form, so I pointed you to the documentation on forms...

    here is some code from a frontend editting form I have, hope this helps instead...

    function MyDataObjectForm() {
       $doMyDataObject = DataObject::get_by_id('MyDataObject',<ID>);
       $fields = $doBusinessProfile->getCMSFields();
       $ts = $fields->findOrMakeTab('Root.Main');
       $fields = $ts->Fields();

       $actions = new FieldSet(new FormAction('CreateUpdateMyForm', 'Update'));

       $form = new Form($this, 'MyEditForm', $fields, $actions, $validator);

       if(Session::get("MyFormData")) {
          $previous_value = Session::get("MyFormData");
          Session::clear("MyFormData");
          $form->loadDataFrom($previous_value);
       }
       else {
          $form->loadDataFrom($doMyDataObject);
       }

       return $form;
    }

    function CreateUpdateMyForm($data,$form) {
       $bValid = true;
       Session::set("MyFormData", $data);

       only do the password change if they both have text in and match
       if($data['SomeField'] != 'SomeValue) {
          $form->addErrorMessage('SomeField','Some custom error','required');
          $bValid = false;
       }

       

       if (!$bValid){
          return Director::redirectBack();
       }

       $doMyDataObject = DataObject::get_by_id('MyDataObject',<ID>);
       $doMyDataObject->update($data);
       $doMyDataObject->write();

       Director::redirect($this->Link().'&posted=1');
    }

  • Rhodin
    Avatar
    Community Member
    3 Posts

    Re: DataObject Link to this post

    Hey, thank you.
    Looks nice, but a bit confusing, I don't have all of your code so I don't know what couple of things are, seems a bit of it missing for the whole picture. I will try to figure it out though. Anyways, how do you call it from template? Where do you use $strURL (i guess that is the url where the form should be rendered or what?

  • swaiba
    Avatar
    Forum Moderator
    1786 Posts

    Re: DataObject Link to this post

    I've removed the $strURL bit as that related to the success page - the current page plus a GET url var to indicate it's been posted to...

    where this can go into the controller along with the functions from above...

    function Posted() {
       if (isset($_GET['posted']))return true;
       return false;
    }

    and the template code...

    <% if Posted %>
    thanks you filling in the form
    <% else %>
    $MyDataObjectForm
    <% end_if %>

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