Jump to:

22997 Posts in 11620 Topics by 2827 members

General Questions

SilverStripe Forums » General Questions » Trouble establishing the proper has_whatever relationship

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: 312 Views
  • Pix
    Avatar
    Community Member
    120 Posts

    Trouble establishing the proper has_whatever relationship Link to this post

    Hi Folks,

    I have a form which will submit a dataobject from the front end, adapted from the nice tutorial found here, and also using the DataObjets as Pages module:

    http://www.ssbits.com/tutorials/2012/s-new-post-78/

    I am using SS3, everything is working nicely, it's very cool and I can submit products. The problem is I am now trying to associate that DataObject with the logged in user, and also be able to generate a list of items submitted by the user. So what I have done:

    In 'Product.php':

    class Product extends DataObjectAsPage
    {
    static $has_one = array(
       "Member" => "Member"
    );


    Then I add a extension to the Member (and register it in config) as follows:

    class MyMember extends DataExtension {
    static $has_many = array(
       "Products" => "Product"
    );


    Then in my form I have:

    $owner=Member::currentUserID();
    // form fields
    $fields = new FieldList(
    new TextField('Title', 'Title :'),
    new HtmlEditorField('Content', 'Content:'),
    new HiddenField('Member', '', $owner),
    );

    All I can see is Product table does have a new "MemberID" field, but no data :0(
    I can't see anything in the Member table that is different either. Also I am lost as to how to retrieve Products by user if in fact I had the relationship working in the first place.

    So in a nutshell: associate DataObjects with a Member, generate a list of their DataObjects. Any pointers would be GREATLY appreciated.

    Disclaimer: I probably just have no idea what I am doing.

    Thanks

  • Pix
    Avatar
    Community Member
    120 Posts

    Re: Trouble establishing the proper has_whatever relationship Link to this post

    OK this is interesting! I see that when I add a product through the Product Admin in the CMS the has_one relationship is established! Somehow, I do not have the method right on my front end form.....any ideas? The form looks like this:

    $owner=Member::currentUserID();
    // form fields
    $fields = new FieldList(
    new TextField('Title', 'Title :'),
    new HtmlEditorField('Content', 'Content:'),
    new HiddenField('Member', '', $owner),
    );

  • Willr
    Avatar
    Forum Moderator
    5462 Posts

    Re: Trouble establishing the proper has_whatever relationship Link to this post

    To assign an object to the parent make sure you set the foreign ID. E.g

    $product->MemberID = Member::currentUserID();
    $product->write();

  • Pix
    Avatar
    Community Member
    120 Posts

    Re: Trouble establishing the proper has_whatever relationship Link to this post

    Thank you Will, that solution indeed works perfectly. Thanks for taking the time to respond. SilverStripe is awesome, I also want to thank you and your team for making this superb CMS and framework. I heavily evaluated over 10 CMS before I started using SilverStripe and nothing compares. I am always delighted what I can build using the framework, nothing else is this flexible. Part of me wants the whole world to know about it, and part of me wants to keep it my own little secret weapon ;0) HA!

  • Pix
    Avatar
    Community Member
    120 Posts

    Re: Trouble establishing the proper has_whatever relationship Link to this post

    Will,

    This is working great, but could you tell me if it would be possible to associate the DataObject with a group instead of an individual member ID, so that members of the group can edit and create the products? Thank you

  • Willr
    Avatar
    Forum Moderator
    5462 Posts

    Re: Trouble establishing the proper has_whatever relationship Link to this post

    This is working great, but could you tell me if it would be possible to associate the DataObject with a group instead of an individual member ID, so that members of the group can edit and create the products?

    Provided you have the relationship setup on your Product

    public static $has_one = array(
    'Editors' => 'Group'
    );

    Then you can assign to that group by doing a search for that group

    $group = DataObject::get('Group', "Code = 'some-group-name'");
    if($group) {
    $product->GroupID = $group->ID;
    $product->write();
    }

    You could also include code to create the group if needed

    if(!$group) {
    $group = new Group();
    $group->Code = 'some-group-name';
    $group->Title = "Some group";
    $group->write();

    $product->GroupID = $group->ID;
    $product->write();
    }

  • Pix
    Avatar
    Community Member
    120 Posts

    Re: Trouble establishing the proper has_whatever relationship Link to this post

    Wow wow! That's awesome! Thank you very much :0)

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