不幸的是,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