Facebook Cookbook - Jay Goldman [125]
The current loggedinuser needs to be allowed to see the specified group or you’ll get back an empty set. Two things to note if you’re looking up a group by name:
Groups aren’t unique by name, so you may well get more than one result back.
Remember that it’s an exact match, so you’ll need to get punctuation and capitalization exactly right.
Retrieving a User’s Groups
Problem
I need to retrieve a specific user’s groups using FQL.
Solution
You can’t get a user’s groups without using both the group and group_member tables:
SELECT name FROM group WHERE gid IN (SELECT gid FROM group_member WHERE uid = $uid);
Discussion
If the current loggedinuser doesn’t have permission to see the groups that uid is in, you’ll get an empty set.
Checking Whether Two Users Are in the Same Group
Problem
I need to check to see whether two specific users are in the same group using FQL.
Solution
Assuming the current loggedinuser is able to see both groups:
SELECT name FROM group WHERE gid IN (SELECT gid FROM
group_member WHERE uid = $user1 AND gid IN (SELECT gid FROM group_member
WHERE uid = $user2))
You’ll need to plug the two IDs into user1 and user2.
Discussion
This should be pretty easy to extend for more than two users if you need to, and it shouldn’t affect performance considerably, since the heavy lifting is done by the database. Just chain on an additional subquery for every additional user you need to check, and remember to add a closing bracket at the end.
Listing Table
Problem
What’s the schema for the listing table?
Solution
The listing table stores the listings created in the Facebook Marketplace application. Its fields are listed in Table 8-13. Queries to this table will only return data the current user is allowed to see (i.e., you can’t request listing that the current loggedinuser can’t see). More information about this table, including an up-to-date listing of fields, can be found at http://wiki.developers.facebook.com/index.php/Listings_(FQL).
Table 8-13. listing table fields
Name
Type
Index
Description
listing_id
int
•
Listing ID of this listing.
url
string
URL pointing to this listing on Facebook.
title
string
Title of this listing.
description
string
Description of this listing.
price
string
Price of this item as a string (rather than a float).
poster
int
•
User ID of the user who posted this item.
update_time
string
The last time this item was updated in epoch seconds. For more info about epoch seconds, see Formatting Relative Time.
category
string
The top-level categories for listings are JOBS, FORSALE, HOUSING, FREESTUFF, and OTHER.
subcategory
string
Subcategories are the level below the category. Each category has about five subs (e.g., “For Sale” breaks down into “Books”, “Furniture”, “Tickets”, “Electronics”, “Cars”, and “Other”).
image_urls
array
An array of the images uploaded to this listing. Users can add four images when they create the listing and can add more after it’s been created (which is a really effective user experience design trick to limit most people to four unless they’re determined enough to add more).
condition
string
Condition of the item, if applicable. Either Used or New.
isbn
string
ISBN number, if applicable.
num_beds
int
Number of bedrooms, if applicable.
num_baths
int
Number of bathrooms, if applicable.
dogs
bool
Are dogs allowed? (if applicable)
cats
bool
Are cats allowed? (if applicable)
smoking
bool
Is smoking allowed? (if applicable)
square_footage
string
Square footage of the property, if applicable.
street
string
Street address of the property, if applicable.
crossstreet
string
Closest cross street to the property, if applicable.
postal
string
Postal/zip code of the property, if applicable.
rent
int
Monthly rent of the property, if applicable.
pay
int
Salary for the job, if applicable.
Note that only the fields marked as “Index” in this table can be used in an FQL query’s WHERE clause,