2508 Posts in 675 Topics by 520 members
Data Model Questions
SilverStripe Forums » Data Model Questions » Newbie to Data Queries
Moderators: martimiz, Howard, Sean, Ryan M., biapar, Willr, Ingo, swaiba
|
Page:
1
|
Go to End | |
| Author | Topic: | 879 Views |
-
Newbie to Data Queries

12 June 2009 at 5:14am Last edited: 12 June 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
-
Re: Newbie to Data Queries

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?
-
Re: Newbie to Data Queries

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!
-
Re: Newbie to Data Queries

23 June 2009 at 9:47am Last edited: 23 June 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.
| 879 Views | ||
|
Page:
1
|
Go to Top |



