Online Book Reader

Home Category

Facebook Cookbook - Jay Goldman [159]

By Root 639 0
types, you can create objects (think rows in a database table) based on them:

// Create an object

$objProps = array('name'=>'Findley','breed'=>1,'bio'=>'Best dog!');

$objId = $facebook->api_client->data_createObject('canine',json_encode($objProps));

// Update it

// true = replace values, fales = merge

$objProps = array('name'=>'Findley','breed'=>1,'bio'=>'Best dog in the world!');

$facebook->api_client->data_updateObject($objId,json_encode($objProps),true);

// Get the properties

$findleyProperties = $facebook->api_client->data_getObject($objId);

$findleyBio = $facebook->api_client->data_getObject($objId,'bio');

The last part of the Data Store API is creating associations, which act as indexes to speed up queries across the distributed tables. Associations can be one-way (a person owns a car but a car doesn’t own a person), symmetrical two-way (a spouse is a spouse in both directions), or asymmetrical two-way (a husband to a wife is not the same as a wife to a husband). Assuming we have an object type called 'person', let’s define the relationship between people and their dogs:

// Define an association

$info1 = array('alias'=>'owner','person',false);

$info2 = array('alias'=>'canine','person',false);

// 1 = one way

// 2 = two way symmetrical

// 3 = two way asymmetrical (requires the other direction to be named)

$facebook->api_client->data_defineAssociation('owner_of_pet',3,

$info1,$info2,'pet_of_owner');

$myAssoc = $facebook->api_client->data_getAssociationDefinition('owner_of_pet');

When we’re done with our associations, we can undefine them (make sure you undefine both sides of a two-way asymmetrical association):

$facebook->api_client->data_undefineAssociation('owner_of_pet');

$facebook->api_client->data_undefineAssociation('pet_of_owner');

As with object types and objects, we can now create actual instances of the association we just defined:

// Create objects

$objProps = array('name'=>'Findley','breed'=>1,'bio'=>'Best dog in the world!');

$dogId = $facebook->api_client->data_createObject('canine',json_encode($objProps));

$objProps = array('name'=>'Jay','bio'=>'Best dog owner in the world!');

$ownerId = $facebook->api_client->data_createObject('person',json_encode($objProps));

// Set an association

$facebook->api_client->data_setAssociation('owner_of_pet',$ownerId,$dogId);

$jaysDogs = $facebook->api_client->data_getAssociatedObjects('owner_of_

pet',$ownerId,false);

$findleysOwners = $facebook->api_client->data_getAssociatedObjects

('pet_of_owner',$dogId,false);

As mentioned earlier, this recipe covers only the basics of the Data Store API, and so you’ll want to consult the Wiki for more detailed info.

Discussion


Although there are some definite advantages in using the Data Store API (mostly around performance and ease of implementation), it also means you can’t share the data that you store in there with any other frontends. If you’re following my architecture advice (see Architecting for the Future: Open Web Apps), you might want to consider storing this information in your own database so that other frontends can share the same info.

Granting Permissions to Other Applications Via the Permissions API


Problem


I’m building a whole family of applications, and I’m writing an additional one for myself that is going to aggregate all of the daily metrics and allocations from its siblings into one convenient place. How can I have one app gather stats for another?

(How’s that for a contrived example? There aren’t many times when you’ll need these calls otherwise, but maybe you have more imagination than I do.)

Solution


The Permissions API, which was still in beta at the time of this writing, enables you to grant and revoke permissions for one app to call three Admin methods on behalf of another app. The three available methods are:

Admin.getAppProperties() (see Getting and Setting Application Properties)

Admin.getMetrics() (see Getting Metrics)

Admin.getAllocations() (see Getting Allocations)

You’ll need to know the API key of the application

Return Main Page Previous Page Next Page

®Online Book Reader