2

特定のパラメーターに従って製品コレクションをロードし、cms/page で使用できる場所に保存する cronjob を Magento で作成したいと考えています。

私の最初のアプローチは、Magento のレジストリを使用することでしたが、それは機能しません。

Mage::register('label',$product_collection);

...「ラベル」が PHTML ファイルの Mage::registry で利用できないように見えるため、機能しません...

誰かが私を正しい方向に向けることができますか? これは正しいアプローチですか?もしそうなら、それを機能させる方法; そうでない場合は、どうすればよいですか?

前もって感謝します!

4

1 に答える 1

4

残念ながら、Mage::register では目的の場所に移動できません。Mage レジストリ キーは、実行中の PHP スクリプトのメモリに保存されるため、PHP コードを実行しているページ リクエストに範囲が限定され、cron と PHTML ファイルの間で共有されません。

探しているものを実現するには、コレクションをハードディスクや Memcache などの永続ストレージにキャッシュする必要があります。次のように、キャッシュする前に特に load() 関数を呼び出す必要がある場合があります。

<?php
// ...
// ... Somewhere in your cron script
$product_collection = Mage::getModel('catalog/product')->getCollection()
    ->addFieldToFilter('some_field', 'some_value');
$product_collection->load(); // Magento kind of "lazy-loads" its data, so
                             // without this, you might not save yourself
                             // from executing MySQL code in the PHTML

// Serialize the data so it can be saved to cache as a string
$cacheData = serialize($product_collection);

$cacheKey = 'some sort of unique cache key';

// Save the serialized collection to the cache (defined in app/etc/local.xml)
Mage::app()->getCacheInstance()->save($cacheData, $cacheKey);

次に、PHTML ファイルで次のことを試してください。

<?php
// ...
$cacheKey = 'same unique cache key set in the cron script';

// Load the collection from cache
$product_collection = Mage::app()->getCacheInstance()->load($cacheKey);

// I'm not sure if Magento will auto-unserialize your object, so if
// the cache gives us a string, then we will do it ourselves
if ( is_string($product_collection) ) {
    $product_collection = unserialize($product_collectoin);
}

// ...

http://www.magentocommerce.com/boards/viewthread/240836を参照してください。

于 2012-11-14T04:27:09.337 に答える