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

Trouble establishing the proper has_whatever relationship


Go to End


7 Posts   739 Views

Avatar
Pix

Community Member, 158 Posts

2 February 2013 at 1:20pm

Edited: 02/02/2013 1:21pm

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

Avatar
Pix

Community Member, 158 Posts

2 February 2013 at 2:32pm

Edited: 02/02/2013 2:33pm

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), 
);

Avatar
Willr

Forum Moderator, 5523 Posts

4 February 2013 at 8:01pm

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

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

Avatar
Pix

Community Member, 158 Posts

5 February 2013 at 9:46am

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!

Avatar
Pix

Community Member, 158 Posts

21 February 2013 at 5:09pm

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

Avatar
Willr

Forum Moderator, 5523 Posts

21 February 2013 at 9:00pm

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();
}

Avatar
Pix

Community Member, 158 Posts

22 February 2013 at 6:21am

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