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.

Data Model Questions /

Newbie to Data Queries


Reply


4 Posts   1207 Views

Avatar
mccarville

Community Member, 32 Posts

12 June 2009 at 5:14am

Edited: 12/06/2009 5:17am

I'm hoping someone might take pity on a newbie to queries in SS. Well to be honest a newbie to queries in general. i have limited SQL exeperience.

Let's say I create the dataobject below

class StaffMember extends DataObject {

static $db = array(
'FirstName' => 'Text',
'Lastname' => 'Text',
'Department' => 'Text'
);

Assuming I have several departments one of which is Sales, and I want to display just the staff members from Sales... I would guess I would write something like

function SalesStaff(){
$SS = DataObject::get("StaffMember", "Department = Sales", "LastName DESC", null, null);
return $SS;
}

and then in the page I want to display it in

<table >
<tr>
<th scope="col">Department</th>
<th scope="col">Last Name</th>
<th scope="col">First Name</th>
</tr>
<% control SalesStaff %>
<tr>
<td>$Department</td>
<td>$LastName</td>
<td>$FirstName</td>
</tr>
<% end_control %>
</table>

Am I anywhere even close??

If anyone has time to analyze my novice ramblings it would be greatly appreciated

Avatar
Matthew

Community Member, 5 Posts

12 June 2009 at 6:44am

I've been battling with this kind of thing for 2 days now, but this is simpler than what i'm trying to do.. so long as your SalesStaff() function is in the controller of the page class associated with that template snippet, looks good to me... are there errors when you try it?

Avatar
CrazyCoders

Community Member, 32 Posts

23 June 2009 at 3:53am

Indeed, this looks perfectly good, are you getting an error? This is THE way to go in SS at least!

Avatar
Hamish

Community Member, 712 Posts

23 June 2009 at 9:47am

Edited: 23/06/2009 9:49am

You have an SQL error.

Change this:

$SS = DataObject::get("StaffMember", "Department = Sales", "LastName DESC", null, null);

to:

$SS = DataObject::get("StaffMember", "Department = 'Sales'", "LastName DESC", null, null);

Note the quote marks around 'Sales'.

Everything else appears to be correct.

Note that if you were in dev mode you should have seen an error that (more or less) explained what the problem was.

[url]http://doc.silverstripe.org/doku.php?id=debugging[/url]