Magento - 相同的代码生成不同的SQL

时间:2012-06-26 14:18:23

标签: magento collections

我有以下代码:

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);
}

这背后的想法是抓住过去两周内添加的所有产品清空最新产品类别,并使用过去两周内添加的所有产品重新填充。

它一直运行良好但最近addLatestProducts()生成的集合似乎生成了2个不同的select语句。有时候它有效..有时却没有。 它生成的两个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

由于

UPDATE *:好的,似乎每个 其他 时间都失败了。第一次运行没关系。第二次运行失败。第3次运行没关系。第四次运行失败。等...

0 个答案:

没有答案