如何保存收藏供以后在Magento中使用?

如何保存收藏供以后在Magento中使用?,magento,cron,registry,Magento,Cron,Registry,我想在Magento中编写一个cronjob,按照某些参数加载产品集合,并将其保存在cms/页面中可以使用的地方 我的第一个方法是使用Magento的注册表,但那不起作用,即一个简单的 Mage::register('label',$product_collection); 。。。不起作用,因为在我的PHTML文件的Mage::registry中似乎“label”不可用 有人能给我指出正确的方向吗?这是正确的方法吗?若有,如何运作;;如果没有,怎么做 提前谢谢 不幸的是,Mage::regis

我想在Magento中编写一个cronjob,按照某些参数加载产品集合,并将其保存在cms/页面中可以使用的地方

我的第一个方法是使用Magento的注册表,但那不起作用,即一个简单的

Mage::register('label',$product_collection);
。。。不起作用,因为在我的PHTML文件的Mage::registry中似乎“label”不可用

有人能给我指出正确的方向吗?这是正确的方法吗?若有,如何运作;;如果没有,怎么做


提前谢谢

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

// ...

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

// ...

为什么您不能直接在CMS页面上加载集合?我已经错过了为什么你首先需要cron工作的要点。我想要一个产品页面,以每天变化的随机顺序显示特定类别的产品,因此我需要生成集合,并确保它在一天中始终保持特定的顺序。我还需要激活分层导航。谢谢@Paulo的解释,但我仍然不认为将整个集合存储在缓存中有什么意义。您只需引入每天都会更改的随机值,然后根据该值决定今天使用的排序。并将您的收藏加载到CMS页面本身。。。但我可能仍然不明白真正的目的。为什么你不能直接把收藏载入CMS页面呢?我已经错过了为什么你首先需要cron工作的要点。我想要一个产品页面,以每天变化的随机顺序显示特定类别的产品,因此我需要生成集合,并确保它在一天中始终保持特定的顺序。我还需要激活分层导航。谢谢@Paulo的解释,但我仍然不认为将整个集合存储在缓存中有什么意义。您只需引入每天都会更改的随机值,然后根据该值决定今天使用的排序。并将您的收藏加载到CMS页面本身。。。但我可能仍然不明白真正的目的。好的观点!谢谢你的提示。我正在使用serialize的过程中,但无法找出缓存部分!在数据量大的情况下,json会占用更少的空间。另外,最好单独保存集合中的每个项目(并且仅保存getData()内容),加载时使用这些数据点填充项目集合!谢谢你的提示。我正在使用serialize的过程中,但无法找出缓存部分!在数据量大的情况下,json会占用更少的空间。另外,最好单独保存集合的每个项(并且仅保存getData()内容),加载时使用这些数据填充项集合