Jump to:

22992 Posts in 11842 Topics by 2828 members

General Questions

SilverStripe Forums » General Questions » has_one has_many from front end form

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

Page: 1
Go to End
Author Topic: 349 Views
  • Pix
    Avatar
    Community Member
    120 Posts

    has_one has_many from front end form Link to this post

    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

  • Pix
    Avatar
    Community Member
    120 Posts

    Re: has_one has_many from front end form Link to this post

    Ummmmm anyone know this?

  • IOTI
    Avatar
    Community Member
    187 Posts

    Re: has_one has_many from front end form Link to this post

    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?

  • Pix
    Avatar
    Community Member
    120 Posts

    Re: has_one has_many from front end form Link to this post

    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)

  • IOTI
    Avatar
    Community Member
    187 Posts

    Re: has_one has_many from front end form Link to this post

    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.

  • Pix
    Avatar
    Community Member
    120 Posts

    Re: has_one has_many from front end form Link to this post

    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!

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