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.

Archive /

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

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

Query Against the SiteTree_Live Table


Go to End


4 Posts   2195 Views

Avatar
mcocolla

Community Member, 4 Posts

27 June 2008 at 4:15am

Edited: 27/06/2008 4:16am

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.

Avatar
Ingo

Forum Moderator, 801 Posts

3 July 2008 at 10:55pm

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)

Avatar
mcocolla

Community Member, 4 Posts

8 July 2008 at 2:11am

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!

Avatar
Ingo

Forum Moderator, 801 Posts

8 July 2008 at 11:32am

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