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.

General Questions

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

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

has_one has_many from front end form


Reply

6 Posts   442 Views

Avatar
Pix

1 August 2013 at 7:41am (Last edited: 2 August 2013 4:03am), Community Member, 146 Posts

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

2 August 2013 at 4:03am Community Member, 146 Posts

Ummmmm anyone know this?

Avatar
IOTI

2 August 2013 at 6:25am (Last edited: 2 August 2013 6:26am), Community Member, 189 Posts

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

2 August 2013 at 12:47pm Community Member, 146 Posts

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
IOTI

3 August 2013 at 12:35am (Last edited: 3 August 2013 12:36am), Community Member, 189 Posts

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

3 August 2013 at 11:53am Community Member, 146 Posts

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!