21282 Posts in 5730 Topics by 2601 members
|
Page:
1
|
Go to End | |
| Author | Topic: | 899 Views |
-
Selecting a random object by cateory

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
-
Re: Selecting a random object by cateory

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()") ;
-
Re: Selecting a random object by cateory

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
-
Re: Selecting a random object by cateory

9 September 2009 at 6:08pm Last edited: 9 September 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. -
Re: Selecting a random object by cateory

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
-
Re: Selecting a random object by cateory

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
-
Re: Selecting a random object by cateory

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 (.). -
Re: Selecting a random object by cateory

11 September 2009 at 11:31am
Thanks. Banal. That was why I was getting confused. MM
| 899 Views | ||
|
Page:
1
|
Go to Top |

