我正在尋找爲我的Facebook集成組件建模數據的最佳方法。爲了這個例子,我們將查看Facebook的相冊。每個用戶都有一個或多個相冊,其中包含一個或多個照片。對於相冊和照片數據定義可以在這裏找到: http://developers.facebook.com/docs/reference/api/album/http://developers.facebook.com/docs/reference/api/photo/如何在PHP中對這些數據建模?
我的主要目標是使用Facebook的圖形API儘可能容易做,我在做的一切,我可以編寫完成兼容特別感興趣。 最初,我的想法是把數據在我自己的類完全相同模型作爲上述文件如定義,專輯:
/** * @property string $id The album ID * @property Profile $from The profile that created this album * @property string $name The title of the album * @property string $description The description of the album * @property string $location The location of the album * @property string $link A link to this album on Facebook * @property string $cover_photo The album cover photo ID * @property string $privacy The privacy settings for the album * @property int $count The number of photos in this album * @property string $type The type of the album: profile, mobile, wall, normal or album * @property string $created_time The time the photo album was initially created (ISO-8601 date-time) * @property array $likes The information about user likes * @property array $photos The Photos in this album * @property array $comments */ class Album extends Graph { /** * The album ID. * @var string */ protected $id; /** * The profile that created this album. * @var Profile */ protected $from; /** * The title of the album. * @var string */ protected $name; /** * The description of the album. * @var string */ protected $description; /** * The location of the album. * @var string */ protected $location; /** * A link to this album on Facebook. * @var string */ protected $link; /** * The album cover photo ID. * Valid URL * @var string */ protected $cover_photo; /** * The privacy settings for the album. * @var string */ protected $privacy; /** * The number of photos in this album. * @var string */ protected $count; /** * The type of the album: profile, mobile, wall, normal or album. * @var string */ protected $type; /** * The time the photo album was initially created. * ISO-8601 date-time * @var string */ protected $created_time; /** * The last time the photo album was updated. * ISO-8601 date-time * @var string */ protected $updated_time; /** * The information about user likes * @var array */ protected $likes; /** * The comments posted on this Album * @var array */ protected $comments; /** * The Photos in this album * @var array Graph\Album\Photo */ protected $photos; /** * Gets a specific album from the graph * @param $id string * @return Album */ public function getAlbum($id) { $this->setFromStdClass($this->getFromGraph("/{$id}")); return $this; } }
使用這種方法我能延遲加載從另一個入口點的照片當/如果他們是必需的,並且一切都很好,但是我無法獲得將從請求返回的照片陣列的代碼完成,因爲我不知道如何製作支持代碼完成的迭代支持的對象,例如$this->photos[0]->...
我也在想它是否是浪費時間擁有所有的屬性,已經有一個從Facebook返回的stcClass
,所以我可以將其存儲在數據對象中,並使用@property
批註和魔術方法從存儲的返回stdClass中獲取數據在一個屬性,如:
/** * @property string $id The album ID * @property Profile $from The profile that created this album * @property string $name The title of the album * @property string $description The description of the album * @property string $location The location of the album * @property string $link A link to this album on Facebook * @property string $cover_photo The album cover photo ID * @property string $privacy The privacy settings for the album * @property int $count The number of photos in this album * @property string $type The type of the album: profile, mobile, wall, normal or album * @property string $created_time The time the photo album was initially created (ISO-8601 date-time) * @property array $likes The information about user likes * @property array $photos The Photos in this album * @property array $comments */ class Album extends Graph { protected $data; public function __get($name) { $method = 'get' . ucfirst($name); if (method_exists($this, $method)) { return $this->{$method}(); } if (property_exists($this->data, $name)) { return $this->data->{$name}; } } }
這爲我節省了大量的時間在宣佈獨立的方法,是更快,因爲我沒有通過返回stdClass的迭代轉換爲我的模型,一般的工作原理相同用於定義每個屬性的代碼完成。
幫助!
有什麼問題在這裏結束了@var SomePhotoClassName,到底是什麼? – JJJ 2012-03-22 13:45:50
問題是,我如何使用PHP對這些數據建模。具體來說,我怎樣才能在代碼完成的PHP中建模。 – GeeH 2012-03-22 13:49:14