Facebook Cookbook - Jay Goldman [126]
Discussion
The design of this table is a little strange in that it has fields to accommodate every possible listing property in one table, rather than splitting them out into tables for each type of listing and using a central table to record the IDs of the children. Sometimes it’s better to denormalize your data for performance reasons, and this approach means that all listings are in a single table with one index on listing_id, which is significantly faster than chaining down through relationships. For more information about denormalization, see Advanced Relational Database Table Optimization or http://en.wikipedia.org/wiki/Denormalization.
If you’d rather use the API to access listings, try the Marketplace.getListings() and Marketplace.search() methods.
Retrieving a Listing
Problem
I need to retrieve specific a listing from the Facebook Marketplace using FQL.
Solution
If you know the listing_id of the listing, it’s as simple as:
SELECT url, title, description, price, poster FROM listing
WHERE listing_id = $listing_id;
If you don’t know the listing_id but it was posted by user and you do know the title, you can do:
SELECT listing_id, description FROM listing WHERE title = $title AND poster = $user;
Discussion
The current loggedinuser needs to be allowed to see the specified listing or you’ll get back an empty set. Two things to note if you’re looking up a listing by title:
Listings aren’t unique by title, 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 Listings
Problem
I need to retrieve all of the listings posted by a specified user using FQL.
Solution
SELECT url, title, description, price FROM listing WHERE poster = $uid;
Discussion
The current loggedinuser needs to be able to see this listings posted by the user you’re querying on, or you’ll get back an empty set.
Retrieving a User’s Friends’ Listings
Problem
I need to retrieve all of a specified user’s friends’ listings using FQL.
Solution
SELECT title, url, description, price, category FROM
listing WHERE poster IN (SELECT uid2 FROM friend WHERE uid1 = $user);
Discussion
Since this is based on a query on the friend table, you can make it only when user is the current loggedinuser.
Retrieving a User’s Listings by Category
Problem
I need to retrieve all of a specified user’s listings that are in a specific category.
Solution
SELECT title,listing_id, description, category FROM listing WHERE
category = $category AND poster = $user;
Discussion
Unfortunately, there’s no way to retrieve all of the listings in a category independently of the poster, as you ultimately need a WHERE clause that acts on either the poster or listing_id fields.
You can easily modify this recipe to search for all of the listings in a category from all of the user’s friends:
SELECT title, url, description, price category FROM listing
WHERE category = $category AND poster IN (SELECT uid2 FROM friend
WHERE uid1 = $user);
Page Table
Problem
What’s the schema for the page table?
Solution
The page table records information about Facebook Pages. Its fields are listed in Table 8-14. Queries to this table will only return data the current user is allowed to see (i.e., you can’t request pages 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/Page_(FQL).
Table 8-14. page table fields
Name
Type
Index
Description
page_id
int
•
Page ID of this Page.
name
string
•
Name of this Page.
pic_small
string
URL of the small picture for this Page (max width of 50 px and max height of 150 px). Might be empty if this field wasn’t set by the creator.
pic_big
string
URL of the big picture for this Page (max width of 200 px and max height of 600 px). Might be