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

has_one has_many from front end form


Go to End


6 Posts   1004 Views

Avatar
Pix

Community Member, 158 Posts

1 August 2013 at 7:41am

Edited: 02/08/2013 4:03am

How do I establish a has_one has_many relationship from a front end form? The goal is that a logged in member submits a form and the 'DO' created is associated with that member's 'account', where 'account' is also an extension of the member. So I am thinking the 'account' needs to be extended to has_may 'widgets', and the 'widget' has_one 'account' or something like that? I've done plenty of tutorials on how to do this from the back end, but I am lost when it comes to a front end form.....

Thanks for any pointers

Avatar
Pix

Community Member, 158 Posts

2 August 2013 at 4:03am

Ummmmm anyone know this?

Avatar
copernican

Community Member, 189 Posts

2 August 2013 at 6:25am

Edited: 02/08/2013 6:26am

Hi Pix,

as far as I know SilverStripe won't "automagically" create the relationship for you. You'll have to setup the form handler to create the dataobject and set the relationship. In terms of code, you would do everything like you would normally.

//a dataextension to the Widget class
class WidgetExtension extends DataExtension {
public static $has_one=array(
'Owner'=>'Account'
);
}

and then in your form handler function you would have something like

public function formHandler(){
$widget = new Widget();
$widget->Owner = Member::currentUserID(); //you should check and make sure that the user is logged in before doing this
}

Does that make sense?

Avatar
Pix

Community Member, 158 Posts

2 August 2013 at 12:47pm

Yes, I think so, thanks. I did something like that once before, but I wasn't adding the 'has_many' side of the relationship. That's what I don't get. So for instance, if I wanted to put in the template:

<% loop Widgets %>
$Title
<% end_loop %>

where the Widgets owned by an account would be generated from the has_many relationship...I hope I am asking this right :0)

Avatar
copernican

Community Member, 189 Posts

3 August 2013 at 12:35am

Edited: 03/08/2013 12:36am

For looping through the widgets, I think using <% loop $Widgets %> would just work. Saving from the back end doesn't do anything special, it just sets the Owner ID for you. I haven't tested it but I think it would work.

As an alternative you could setup a custom function on your Account class like so

public class Account extends DataExtension {

public function getAccountWidgets(){
if(Widgets::get()->where('OwnerID= ' .$this->owner->ID)->count())
$widgets = Widgets::get()->where('OwnerID= ' .$this->owner->ID);
return $widgets;
}

}

and then in your template use <% loop AccountWidgets %> instead of <% loop Widgets %>. You would of course have to do this within a <% loop Member %> for it to work.

Avatar
Pix

Community Member, 158 Posts

3 August 2013 at 11:53am

That's awesome! I am going to give it a go. I can see how the function you provide would absolutely do the trick too. Thanks for your help!