Jump to:

3377 Posts in 1001 Topics by 714 members

Data Model Questions

SilverStripe Forums » Data Model Questions » Querying Group_Members Table

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

Page: 1
Go to End
Author Topic: 1411 Views
  • IOTI
    Avatar
    Community Member
    187 Posts

    Querying Group_Members Table Link to this post

    I understand that you can't use DataObject::get on Group_Members because its a relational table with no classname column(thats my understanding at least)

    What i want to do is get the GroupID where MemberID equals a certain MemberID depending on who is logged on. I'm trying to figure out how to do this easily. Do i need to use joins or can I just use a DB:Query?

    any help would be appreciated.

  • Willr
    Avatar
    Forum Moderator
    5464 Posts

    Re: Querying Group_Members Table Link to this post

    well if you just want ID's then you can just use something like DB::query("SELECT GroupID FROM Group_Members WHERE MemberID = '1'")

  • IOTI
    Avatar
    Community Member
    187 Posts

    Re: Querying Group_Members Table Link to this post

    I tried using that but it didn't seem to return anything, maybe i was doing something wrong.

    Heres a snippet of my code

    //getting current memberID
    $memberID = Member::currentUserID();
    //using rawquery to get groupID
    $groupMemberID = DB::query("SELECT GroupID FROM Group_Members WHERE MemberID =". $memberID);

    $groupMember = dataobject::get_one('Group', "ID = ". $groupMemberID);

    $parentID = $groupMember->getField('ParentID');

    Using this code, i get a couldnt run query SQL error "FROM "Group" WHERE (ID = ) LIMIT 1" it looks as though $groupMemberID is returning NULL. Is there some other step(s) I am missing?

  • Willr
    Avatar
    Forum Moderator
    5464 Posts

    Re: Querying Group_Members Table Link to this post

    DB::query() returns you a query object. You then have to call the various methods on it to get the raw data. For example the query DB::query("SELECT GroupID FROM Group_Members WHERE MemberID =". $memberID); would return a single column of ID's. If you wanted the top left most value (top group ID) you would call value(); on it

    DB::query("SELECT GroupID FROM Group_Members WHERE MemberID =". $memberID)->value();

    If you wanted a column of all the ID's in an array you call column() which returns the left most column

    DB::query("SELECT GroupID FROM Group_Members WHERE MemberID =". $memberID)->column();

    http://api.silverstripe.org/2.4/sapphire/model/SS_Query.html

  • IOTI
    Avatar
    Community Member
    187 Posts

    Re: Querying Group_Members Table Link to this post

    perfect, thank you, Willr.

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