Jump to:

17452 Posts in 4473 Topics by 1971 members

Archive

SilverStripe Forums » Archive » Query Against the SiteTree_Live Table

Our old forums are still available as a read-only archive.

Moderators: martimiz, Sean, biapar, Willr, Ingo, simon_w

Page: 1
Go to End
Author Topic: 1574 Views
  • mcocolla
    Avatar
    Community Member
    4 Posts

    Query Against the SiteTree_Live Table Link to this post

    I am trying to query the sitetree to return the Title entry of a specific classname "Discipline".

    I'm trying to create a drop downfield for a search that will contain the Disciplines.

    One of the examples specifically points out the map() function to do this as documented here http://doc.silverstripe.com/doku.php?id=sqlquery#mapping

    I've created a function that will return an new search form under the
    class TeacherHolder_Controller extends Page_Controller

    As far as I can tell from the documentation I should be able to use DB:query.

    $discipline = DB::query("Select Title FROM SiteTree_Live WHERE ClassName = 'Discipline' and ShowInMenus = 1 ORDER BY Sort ASC");
    $map = $discipline->execute()->map();

    or

    $sqlQuery = new SQLQuery();
    $sqlQuery->select = array('Title');
    $sqlQuery->from = 'SiteTree';
    $sqlQuery->where = 'ClassName = "Discipline"';
    $sqlQuery->orderby = 'Sort ASC';
    $map = $sqlQuery->execute()->map();

    and then the dropdown creation.

    new DropdownField("Discipline", "Discipline",$map);

    When I post either of those codes to the server I receive this error:


    Error

    The website server has not been able to respond to your request.

    Can anyone shed some light on what I may have done incorrectly. Thanks.

  • Ingo
    Avatar
    Forum Moderator
    801 Posts

    Re: Query Against the SiteTree_Live Table Link to this post

    How about this?

    $pages = DataObject::get('Discipline');
    $map = ($pages && $pages->Count()) ? $pages->toDropDownMap() : false;
    new DropdownField("Discipline", "Discipline",$map);

    P.S: The error message you're getting is not really helpful to anybody on the forum, please make sure you're running your site in "dev-mode" (see http://doc.silverstripe.com/doku.php?id=debugging)

  • mcocolla
    Avatar
    Community Member
    4 Posts

    Re: Query Against the SiteTree_Live Table Link to this post

    Thanks Ingo,
    I did come across a documentation posting regarding the toDropDownMap function and did come up with a similar solution before I was able to check back here.

    I do have a question about your structure when creating the $map variable

    $map = ($pages && $pages->Count()) ? $pages->toDropDownMap() : false;

    What is the ($pages && $pages->Count() doing as my php is a bit rusty. I know it's along the lines of an if statement or return false. I just went straight to $pages->toDropDownMap();

    And I will definitely remember to put the site into debug mode in the future. Thanks!

  • Ingo
    Avatar
    Forum Moderator
    801 Posts

    Re: Query Against the SiteTree_Live Table Link to this post

    > What is the ($pages && $pages->Count() doing as my php is a bit rusty
    it evaluates the two conditions to see if you've actually got items in the returned DataObjectSet. if the condition evaluates true, it will take the stuff before the colon, otherwise after colon (http://en.wikipedia.org/wiki/Ternary_operator)

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