はい!私は実際にこれを Recurly 用に構築しました - オープンソース化しようとしていますが、まだオープンではありません。これは、その根幹である load() メソッドのスニペットです。
// TBT_Recurly_Model_Resource_Recurly_Abstract_Collection
public function load($printQuery = false, $logQuery = false)
{
if ($this->isLoaded()) {
return $this;
}
if ($this->_isCached()) {
return $this->_loadCache();
}
$this->_beforeLoad();
$this->_renderFilters()
->_renderOrders()
->_renderLimit();
$this->clear();
try {
# This is ultimately doing the API call
$recurly_list = $this->_getListSafe();
} catch (Recurly_Error $e) {
Mage::logException($e);
$this->setConnectionError($e->getMessage());
return $this;
}
foreach ($recurly_list as $recurly_item)
{
$item = $this->getNewEmptyItem();
$item->getResource()->setDataOnObject($item, $recurly_item);
// Recurly appears to sometimes return duplicate subscription items in it's list response.
if (!isset($this->_items[$item->getId()])) {
$this->addItem($item);
}
}
$this->_afterLoadRecurly();
// We have to setIsLoaded before we saveCache b/c otherwise it will infinite loop
$this->_setIsLoaded();
$this->_saveCache();
$this->_afterLoad();
return $this;
}
私たちは実際にこれを取得してベース REST クラスに入れましたが、その上に新しい REST API を実装するのが本当に簡単になったので、本当にクールでした。
ベストプラクティスに関する限り、あなたの質問に具体的に答えたかどうかはわかりません。しかし、基本的に、それをきれいにするためにすべき主なことは次のとおりだと思います。
- クエリについては、Magento モデル / コレクション メソッドのシグネチャに従ってください。
- キャッシングを実装する
- リソース モデル レイヤーに API 通信を実装する