Facebook Cookbook - Jay Goldman [158]
Associations
If you’ve read Chapter 8 and checked out some of the performance recommendations around indexing or already have a good level of database design knowledge, you’ll be familiar with how indexing can deliver a substantial speed increase. This is true in a traditional database but loses its effectiveness in a distributed scheme because there’s no centralized directory telling the index where to find data. Facebook has solved this problem with associations, which describe the relationship between two pieces of data (e.g., a husband and wife or a gift giver and receiver).
Since this API will appeal only to a smaller subset of readers, and since it’s in early beta and subject to change, I’m only going to cover a few quick examples and leave the rest up to the Wiki’s documentation, which you’ll find at http://wiki.developers.facebook.com/index.php/Data_Store_API_documentation.
To store and retrieve a user preference:
$prefId = 31;
$prefValue = 'black';
$setResult = $facebook->api_client->data_setUserPreference($prefId, $prefValue);
$getResult = $facebook->api_client->data_getUserPreference($prefId);
You can store up to 201 preferences per user per application, identified by the numeric IDs 0–200. One important note: the Data Store API will clear out a preference set to either zero (0) or an empty string, which it treats as the same thing. If you need to set a preference to the value 0, you’ll need to come up with a scheme that you’ll later recognize, since setting it to 0 will remove it (e.g., “n:0”, or “zero”, or “nil”, etc.). Both of the methods shown in the code sample also have a sibling batch mode version: Data.setUserPreferences() and Data.getUserPreferences(). The former takes an associative array of key/value pairs and a boolean indicating whether new preferences should overwrite (true) or merge into (false) existing ones:
$prefBatch = array(31=>'red', 42=>'potato', 68=>'rubber golve');
$setResult = $facebook->api_client->data_setUserPreferences($prefBatch, true);
$getResult = $facebook->api_client->data_getUserPreferences();
There are no arguments for Data.getUserPreferences(), which will return a multidimensional array containing all of the preferences for this user in this app:
Array
(
[0] => Array
(
[pref_id] => 31
[value] => red
)
[1] => Array
(
[pref_id] => 42
[value] => potato
)
[2] => Array
(
[pref_id] => 68
[value] => rubber golve
)
)
The term “object type” in the Data Store API is effectively what you would think of as a “table” in a traditional database. You can create as many object types as you’d like and can then add as many properties (i.e., fields or columns) as you need. Here’s some sample code for defining a new object type for storing a dog, adding properties, getting a description of our new type, and then dropping it. Note that property names can be up to 32 characters long and can consist of lowercase letters (a–z), numbers (0–9), and underscores. There are currently three supported types: integers (1), strings (2), and blobs of text (3):
// Create the Object Type
$facebook->api_client->data_createObjectType('dog');
// Define some Properties
$facebook->api_client->data_defineObjectProperty('dog','name',2); // 2 = string
$facebook->api_client->data_defineObjectProperty('dog','breed',1); // 1 = int
$facebook->api_client->data_defineObjectProperty('dog','cat_friendly',1);
$facebook->api_client->data_defineObjectProperty('dog',
'description',3); // 3 = text blob
// Make some changes
$facebook->api_client->data_renameObjectProperty('dog','description','bio');
$facebook->api_client->data_undefineObjectProperty('dog',
'cat_friendly');
$facebook->api_client->data_renameObjectType('dog','canine');
// Retrieve the type(s)
$objectTypes = $facebook->api_client->data_getObjectTypes(); // Get all types
$myObjectType = $facebook->api_client->data_getObjectType('canine'); // Get the type
// Drop it
$facebook->api_client->data_dropObjectType('canine'); // Bye!
Once you’ve created your object