我有以下代码:
set_time_limit(7200);
error_reporting(E_ALL);
ini_set('display_errors','On');
$start = microtime_float();
require_once 'app/Mage.php';
Mage::app();
$storeId = array(1);
// Empty Latest Products
$_category = Mage::getModel('catalog/category')->loadByAttribute('name', 'Latest Products');
$latestProductsId = $_category->getId();
emptyCategory($latestProductsId);
addLatestProducts($latestProductsId);
function emptyCategory($catId)
{
$category = Mage::getModel("catalog/category")->load($catId);
$collection = $category->getProductCollection();
foreach($collection as $product)
{
$curCats = $product->getCategoryIds();
$changed = false;
foreach($curCats as $key => $value)
{
if($value == $catId)
{
unset($curCats[$key]); // remove the category
$changed = true;
}
}
if($changed) // only save the product if nessesary
{
$product->setCategoryIds($curCats);
$product->save();
}
}
}
function addLatestProducts($latestProductsId)
{
$tomorrow = date('Y-m-d 00:00:00', strtotime("tomorrow"));
$nWeeksAgo = date('Y-m-d 00:00:00', strtotime("-2 weeks"));
$visibility = array(Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH, Mage_Catalog_Model_Product_Visibility::VISIBILITY_IN_CATALOG);
$status = array(Mage_Catalog_Model_Product_Status::STATUS_ENABLED);
$storeId = 1;
$_productCollection = Mage::getResourceModel('catalog/product_collection')
->addAttributeToSelect('*')
->addAttributeToFilter('visibility', $visibility)
->addAttributeToFilter('status', $status)
->setStoreId($storeId)
->setPageSize(30)
->setOrder('entity_id', 'DESC')
->addAttributeToFilter('created_at', array(
'from' => $nWeeksAgo,
'to' => $tomorrow,
'date' => true, // specifies conversion of comparison values
));
$i=0;
echo $_productCollection->getSelect(); //<---- This is where the SQL is generated
foreach ($_productCollection as $product) {
$curCats = $product->getCategoryIds();
if(!in_array($latestProductsId, $curCats)){
// not currently in the latest products category. Add it.
$curCats[] = $latestProductsId;
$product->setCategoryIds($curCats);
$product->save();
unset($product);
}
$i++;
}
unset($_productCollection);
}
这背后的想法是获取过去 2 周内添加的所有产品,清空“最新产品”类别,并用过去 2 周内添加的所有产品重新填充它。
它一直运行良好,但最近 addLatestProducts() 生成的集合似乎生成了 2 个不同的选择语句。有时它有效..有时它没有。它生成的两个 SQL 位是:
SELECT `e`.*,
IF(_table_visibility.value_id > 0, _table_visibility.value, _table_visibility_default.value) AS `visibility`,
IF(_table_status.value_id > 0, _table_status.value, _table_status_default.value) AS `status`,
IFNULL(`entity_id_option_value_t2`.`value`, `entity_id_option_value_t1`.`value`) AS `entity_id_value`
FROM `catalog_product_entity` AS `e`
INNER JOIN `catalog_product_entity_int` AS `_table_visibility_default` ON (`_table_visibility_default`.`entity_id` = `e`.`entity_id`) AND (`_table_visibility_default`.`attribute_id` = '91') AND `_table_visibility_default`.`store_id` = 0
LEFT JOIN `catalog_product_entity_int` AS `_table_visibility` ON (`_table_visibility`.`entity_id` = `e`.`entity_id`) AND (`_table_visibility`.`attribute_id` = '91') AND (`_table_visibility`.`store_id` = '1')
INNER JOIN `catalog_product_entity_int` AS `_table_status_default` ON (`_table_status_default`.`entity_id` = `e`.`entity_id`) AND (`_table_status_default`.`attribute_id` = '84') AND `_table_status_default`.`store_id` = 0
LEFT JOIN `catalog_product_entity_int` AS `_table_status` ON (`_table_status`.`entity_id` = `e`.`entity_id`) AND (`_table_status`.`attribute_id` = '84') AND (`_table_status`.`store_id` = '1')
LEFT JOIN `catalog_product_entity` AS `entity_id_t1` ON `e`.`entity_id`=`entity_id_t1`.`entity_id` AND `entity_id_t1`.`attribute_id`='' AND `entity_id_t1`.`store_id`='0'
LEFT JOIN `catalog_product_entity` AS `entity_id_t2` ON `e`.`entity_id`=`entity_id_t2`.`entity_id` AND `entity_id_t2`.`attribute_id`='' AND `entity_id_t2`.`store_id`='1'
LEFT JOIN `eav_attribute_option_value` AS `entity_id_option_value_t1` ON `entity_id_option_value_t1`.`option_id`=IF(`entity_id_t2`.`value_id`>0, `entity_id_t2`.`value`, `entity_id_t1`.`value`) AND `entity_id_option_value_t1`.`store_id`='0'
LEFT JOIN `eav_attribute_option_value` AS `entity_id_option_value_t2` ON `entity_id_option_value_t2`.`option_id`=IF(`entity_id_t2`.`value_id`>0, `entity_id_t2`.`value`, `entity_id_t1`.`value`) AND `entity_id_option_value_t1`.`store_id`='1'
WHERE ((
(IF(_table_visibility.value_id > 0, _table_visibility.value, _table_visibility_default.value) = '4')
or (IF(_table_visibility.value_id > 0, _table_visibility.value, _table_visibility_default.value) = '2')
))
AND (((IF(_table_status.value_id > 0, _table_status.value, _table_status_default.value) = '1')))
AND (e.created_at >= '2012-06-12' and e.created_at <= '2012-06-27')
ORDER BY `entity_id_value` DESC
和
SELECT `e`.*,
IF(_table_visibility.value_id > 0, _table_visibility.value, _table_visibility_default.value) AS `visibility`,
IF(_table_status.value_id > 0, _table_status.value, _table_status_default.value) AS `status`
FROM `catalog_product_entity` AS `e`
INNER JOIN `catalog_product_entity_int` AS `_table_visibility_default` ON (`_table_visibility_default`.`entity_id` = `e`.`entity_id`) AND (`_table_visibility_default`.`attribute_id` = '91') AND `_table_visibility_default`.`store_id` = 0
LEFT JOIN `catalog_product_entity_int` AS `_table_visibility` ON (`_table_visibility`.`entity_id` = `e`.`entity_id`) AND (`_table_visibility`.`attribute_id` = '91') AND (`_table_visibility`.`store_id` = '1')
INNER JOIN `catalog_product_entity_int` AS `_table_status_default` ON (`_table_status_default`.`entity_id` = `e`.`entity_id`) AND (`_table_status_default`.`attribute_id` = '84') AND `_table_status_default`.`store_id` = 0
LEFT JOIN `catalog_product_entity_int` AS `_table_status` ON (`_table_status`.`entity_id` = `e`.`entity_id`) AND (`_table_status`.`attribute_id` = '84') AND (`_table_status`.`store_id` = '1')
WHERE ((
(IF(_table_visibility.value_id > 0, _table_visibility.value, _table_visibility_default.value) = '4')
or (IF(_table_visibility.value_id > 0, _table_visibility.value, _table_visibility_default.value) = '2')
))
AND (((IF(_table_status.value_id > 0, _table_status.value, _table_status_default.value) = '1')))
AND (e.created_at >= '2012-06-12' and e.created_at <= '2012-06-27')
ORDER BY `e`.`entity_id` DESC, `e`.`entity_id` DESC
最上面的 SQL 失败,第二个有效,但它们都是从 5 秒后运行的相同代码生成的。(只需在浏览器中点击刷新!)
知道是什么原因造成的吗?Magento 1.5.1.0
谢谢
更新*:好的,所以它似乎每隔一次都会失败。第一次运行没问题。第二次运行失败。第三次运行就OK了。第 4 次运行失败。ETC...