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.

General Questions /

General questions about getting started with SilverStripe that don't fit in any of the categories above.

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

GenericViews CRUD


Go to End


3 Posts   1389 Views

Avatar
nicanor

Community Member, 15 Posts

29 July 2011 at 2:02am

Edited: 29/07/2011 2:03am

I need froms which supplies CRUD in fronend. No problem to get it working in the backend. I just testetd GenericViews, but it just works for the action view. When i try to customize the url like this

form/8/view to

form/8/edit

i got an empty page. Silverstripe Version is 2.4.5

So i tried out how to make it by my own,
the form Page looks liek this. I tried to get the data after creation back into the form fields.

i save the ID in "createPerson" with Session::set('ID',$submission->ID);

and get it back in myForm with DataObject::get_by_id('HorseSubmission',$id);

is it the right way? How to updated data for an special ID?


class FormPage extends Page {
    static $db = array(
    );

		static $has_one = array(
    );

}
 
class FormPage_Controller extends Page_Controller {
   function myForm() {   	
   			if(Session::get("ID"))
      	{
      		$id = Session::get("ID");
      		$record = DataObject::get_by_id('HorseSubmission',$id);

		      $fields = new FieldSet(
						new TextField('Person','Person',$record->Person),
 						new ImageField('Image1','Bild',$record->Image1),
 						new ImageField('Image2','Bild',$record->Image2),
        	);
         
        	// Create actions
        	$actions = new FieldSet(
            new FormAction('createHorse', 'Submit')
        	);
     
        	return new Form($this, 'myForm', $fields, $actions);
      	}	//end if 
      	else
      	{
        	$fields = new FieldSet(              
						new TextField('Name'),
 						new ImageField('Image1'),
 						new ImageField('Image2')
        	);
         
        $actions = new FieldSet(
            new FormAction('createPerson', 'Submit')
        );
     
        return new Form($this, 'myForm', $fields, $actions);
       } // end else
    }
    
       function createPerson($data, $form) {
                $submission = new PersonSubmission();
                $form->saveInto($submission);
                $submission->write();
                Session::set('ID',$submission->ID);
	        Director::redirectBack();      
       }
}

Avatar
nicanor

Community Member, 15 Posts

29 July 2011 at 3:13am

To update a Person i added a method "updatePerson". It's the same like "createPerson" above. I've just added the line $submission->ID = Session::get("ID");
So it's possible to specify the ID before ->write()

Is this really the way you should solve a CRUD funcionality in SilverStripe?
For me it seems not bad, but really different from other Solutions.
The ID's are stored in OS temp dir, in Silverstripe cache -> Session
Nothing, no Id, no data is visible for the user - correct me i am wrong.

function updatePerson($data, $form) {
        $submission = new PersonSubmission();
        $form->saveInto($submission);
        $submission->ID = Session::get("ID");
        $submission->write();
        Session::set('ID',$submission->ID);
	Director::redirectBack();      
}

Avatar
zenmonkey

Community Member, 545 Posts

29 July 2011 at 7:30am

Not totally sure I get what you're trying to do, as we'd need to see your actual data object and not just the form. But the easiest way to pass the an ID from a Form to an action is to place it a hidden field. If you're editing an existing submission you can use (using you current method of storing the ID):

$id = Session::get("ID");
$submission = DataObject::get_by_id("PersonSubmission", $id);
$form->saveInto($submission);
$submission->write();

Since the object record already exists you just need to pull it from the DB before saving into. Take a look at this page http://doc.silverstripe.org/sapphire/en/topics/datamodel#update I left out the if statement so you may want to add it back in