21301 Posts in 5735 Topics by 2603 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, Howard, Sean, Ryan M., biapar, Willr, Ingo, swaiba, simon_w
|
Page:
1
|
Go to End | |
| Author | Topic: | 184 Views |
-
Trouble establishing the proper has_whatever relationship

2 February 2013 at 1:20pm Last edited: 2 February 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
-
Re: Trouble establishing the proper has_whatever relationship

2 February 2013 at 2:32pm Last edited: 2 February 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),
); -
Re: Trouble establishing the proper has_whatever relationship

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(); -
Re: Trouble establishing the proper has_whatever relationship

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!
-
Re: Trouble establishing the proper has_whatever relationship

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
-
Re: Trouble establishing the proper has_whatever relationship

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();
} -
Re: Trouble establishing the proper has_whatever relationship

22 February 2013 at 6:21am
Wow wow! That's awesome! Thank you very much :0)
| 184 Views | ||
|
Page:
1
|
Go to Top |


