nickspiel
nickspiel

Reputation: 5630

Managing has_one relation inline in SilverStripe

I am wondering if it is possible to directly edit values of a has_one relationship in SilverStripe.

For example:

Person (extension of SiteTree) has one Job (extension of DataObject) where Job has the db fields of title, salary, location etc.

Is it possible in the CMS to place a form in a new tab to edit the values of Job form the Person page? So all of the form fields from the Job DataObject are placed on the Person page and will save to the Job table when I hit save on the Person page?

I can get this to work with inline gridfield editing (thanks to the editable columns class in gridfieldextensions) but I believe this requires a has_many / many_many relationship? It also is not suitable for adding images and managing objects that have a large number of fields.

I hope that make sense. Let me know if you need more clarification.

EDIT: I found this hasonefield module which is very out of date but does 80% of what I am after, it would be amazing if it didn't take you off the page though and you could edit the fields within the parent page (Person).

Upvotes: 2

Views: 1186

Answers (1)

wmk
wmk

Reputation: 4626

there is a fork of simon's orginal has-one-edit module that is still available and composer installable, which does the logic for editing fields (or all fields) of a has_one relation. Install it running

composer require stevie-mayhew/hasoneedit:1.0.x@stable

Then you can define several fields of your has one relation, naming should be HasOneName-_1_-FieldName.

From the docs:

For example, say you have a has_one called Show and that has_one has a field called Title you want to edit. You'd add the field TextField::create('Show-_1_-Title', 'Show Title').

To add support to your own forms, you need to add the sgn_hasoneedit_UpdateFormExtension extension to your controller and call $this->extend('updateEditForm', $form) before returning the form to the template. Without this, the fields will not get populated with the values from the has_one though saving will work.

Upvotes: 2

Related Questions