Jump to:

23550 Posts in 19336 Topics by 2890 members

General Questions

SilverStripe Forums » General Questions » Selecting a random object by cateory

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: 1063 Views
  • mschiefmaker
    Avatar
    Community Member
    187 Posts

    Selecting a random object by cateory Link to this post

    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

  • banal
    Avatar
    Community Member
    901 Posts

    Re: Selecting a random object by cateory Link to this post

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

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

  • mschiefmaker
    Avatar
    Community Member
    187 Posts

    Re: Selecting a random object by cateory Link to this post

    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

  • mschiefmaker
    Avatar
    Community Member
    187 Posts

    Re: Selecting a random object by cateory Link to this post

    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.

  • banal
    Avatar
    Community Member
    901 Posts

    Re: Selecting a random object by cateory Link to this post

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

    'MFCategoryID = ' . $MFoptions->ID

  • mschiefmaker
    Avatar
    Community Member
    187 Posts

    Re: Selecting a random object by cateory Link to this post

    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

  • banal
    Avatar
    Community Member
    901 Posts

    Re: Selecting a random object by cateory Link to this post

    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 (.).

  • mschiefmaker
    Avatar
    Community Member
    187 Posts

    Re: Selecting a random object by cateory Link to this post

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

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