Jump to:

23552 Posts in 19349 Topics by 2891 members

General Questions

SilverStripe Forums » General Questions » Retreiving an object whose category is the same as the page title

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

    Retreiving an object whose category is the same as the page title Link to this post

    Hi

    I am trying to retrieve an object that has a category the same as the page title. I am using the code

    $MFoptions = DataObject::get_one("MFCategory", 'MFTitle='.$this->title);

    but this errors with the message [User Error] Couldn't run query: SELECT `MFCategory`.*, `MFCategory`.ID, if(`MFCategory`.ClassName,`MFCategory`.ClassName,'MFCategory') AS RecordClassName FROM `MFCategory` WHERE (MFTitle=counselling) LIMIT 1 Unknown column 'counselling' in 'where clause'

    So I get it is looking for a column called counselling cause it is not in '' but when I put it in '' ie $MFoptions = DataObject::get_one("MFCategory", 'MFTitle='.'$this->title');

    it interprets the $this->title as a literal string. Please help, I know this must be simple to fix but I just can't work it out.

    Thanks as always

    MM

  • banal
    Avatar
    Community Member
    901 Posts

    Re: Retreiving an object whose category is the same as the page title Link to this post

    You're close to the solution. The Title has to be wrapped in quotes for MySQL, but you're doing it wrong. Should be:

    $MFoptions = DataObject::get_one("MFCategory", "MFTitle='" . $this->title . "'");

    Or maybe better to read:

    $title = $this->title;
    $MFoptions = DataObject::get_one("MFCategory", "MFTitle='$title'");

  • mschiefmaker
    Avatar
    Community Member
    187 Posts

    Re: Retreiving an object whose category is the same as the page title Link to this post

    Hi Banal

    Thanks for this. I knew it must be something along these lines but the combinations I had tried just didn't work

    Cheers

    MM

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