Jump to:

3373 Posts in 998 Topics by 712 members

Data Model Questions

SilverStripe Forums » Data Model Questions » Newbie to Data Queries

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

Page: 1
Go to End
Author Topic: 1115 Views
  • mccarville
    Avatar
    Community Member
    32 Posts

    Newbie to Data Queries Link to this post

    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

  • Matthew
    Avatar
    Community Member
    5 Posts

    Re: Newbie to Data Queries Link to this post

    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?

  • CrazyCoders
    Avatar
    Community Member
    32 Posts

    Re: Newbie to Data Queries Link to this post

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

  • Hamish
    Avatar
    Community Member
    712 Posts

    Re: Newbie to Data Queries Link to this post

    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.

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

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