3070 Posts in 869 Topics by 651 members
|
Page:
1
|
Go to End | |
| Author | Topic: | 1238 Views |
-
Querying Group_Members Table

31 August 2010 at 2:42am
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.
-
Re: Querying Group_Members Table

31 August 2010 at 8:32pm
well if you just want ID's then you can just use something like DB::query("SELECT GroupID FROM Group_Members WHERE MemberID = '1'")
-
Re: Querying Group_Members Table

1 September 2010 at 1:10am
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?
-
Re: Querying Group_Members Table

1 September 2010 at 10:08am
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
| 1238 Views | ||
|
Page:
1
|
Go to Top |


