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

Selecting a random object by cateory


Go to End


8 Posts   1464 Views

Avatar
mschiefmaker

Community Member, 187 Posts

8 September 2009 at 9:53pm

I want to select a random object from a a table, MonkeysFist where the selection is restricted by a category with maps to the title of the page. So far I have:

function RandomFist() {
$MFoptions = DataObject::get_one("MFCategory","title=Title");
return DataObject::get_one("MonkeysFist","","MFCategoryID = $MFoptions.ID","RAND()") ;
}

This returns a random object but the from any category not restricted as I need to a specific one. What am I doing wrong?

Thanks

MM

Avatar
bummzack

Community Member, 904 Posts

9 September 2009 at 12:03am

You got the get_one parameters wrong.
Should be like this:

return DataObject::get_one("MonkeysFist", "MFCategoryID = $MFoptions.ID", true, "RAND()") ; 

Avatar
mschiefmaker

Community Member, 187 Posts

9 September 2009 at 5:20pm

Thanks for this but I am still having problems. Changing the code as suggested gives me "The website server has not been able to respond to your request." error.

As I say the issue does not appear to be in this part of the code, I get a random object back with my original code but it is not restricted to the category as defined in the first line.

Any other thoughts would be appreciated

MM

Avatar
mschiefmaker

Community Member, 187 Posts

9 September 2009 at 6:08pm

Edited: 09/09/2009 6:09pm

Having tested return
DataObject::get_one("MonkeysFist","MFCategoryID=2",true, "RAND()");
which works as it should this suggests I have something wrong in
MFCategoryID = $MFoptions.ID
and I am wondering if it is $MFoptions.ID but I can't see what.

Avatar
bummzack

Community Member, 904 Posts

9 September 2009 at 7:49pm

You're right. That won't work.. sorry for not spotting it the first time. It should read:

'MFCategoryID = ' . $MFoptions->ID

Avatar
mschiefmaker

Community Member, 187 Posts

9 September 2009 at 8:27pm

Hi Banal

Thanks that solves the problem. Can you just tell me when should I be using .$MFoptions->ID and when $MFoptions.ID

Thanks

MM

Avatar
bummzack

Community Member, 904 Posts

9 September 2009 at 9:54pm

Well, actually you can't use $MFoptions.ID, it's not valid PHP Code. In PHP, accessing members of an Object always requires the -> operator (except static members of course).
What might have confused you is the syntax used in SilverStripe Templates. There you access members of an Object using the dot (.).

Avatar
mschiefmaker

Community Member, 187 Posts

11 September 2009 at 11:31am

Thanks. Banal. That was why I was getting confused. MM