当我用csv更新表时,等待时间超过了


The waiting time is exceeded when i'm updating the tables with a csv

我有这个脚本,它工作,我可以对数量进行更新。唯一的问题是csv文件太大了…我试图设置内存限制,但当我加载页面时,服务器说"等待时间超过了。位于www.auto-univers.fr的服务器响应时间过长。"

我能做什么?

是否可以将行从1更新为1000 ?我确实喜欢他在两个不同的页面上的两个更新,第1行到第1000行和第1000行到最后一行。

<?php
$mageFilename = 'app/Mage.php';
require_once $mageFilename;
Mage::setIsDeveloperMode(true);
ini_set('display_errors', 1);
umask(0);
Mage::app('admin');
Mage::register('isSecureArea', 1);
Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
set_time_limit(0);
ini_set('memory_limit','2048M');

function _getConnection($type = 'core_read'){
    return Mage::getSingleton('core/resource')->getConnection($type);
}
function _getTableName($tableName){
    return Mage::getSingleton('core/resource')->getTableName($tableName);
}
function _getAttributeId($attribute_code = 'price'){
    $connection = _getConnection('core_read');
    $sql = "SELECT attribute_id
                FROM " . _getTableName('eav_attribute') . "
            WHERE
                entity_type_id = ?
                AND attribute_code = ?";
    $entity_type_id = _getEntityTypeId();
    return $connection->fetchOne($sql, array($entity_type_id, $attribute_code));
}
function _getEntityTypeId($entity_type_code = 'catalog_product'){
    $connection = _getConnection('core_read');
    $sql        = "SELECT entity_type_id FROM " . _getTableName('eav_entity_type') . " WHERE entity_type_code = ?";
    return $connection->fetchOne($sql, array($entity_type_code));
}
function _checkIfSkuExists($sku){
    $connection = _getConnection('core_read');
    $sql        = "SELECT COUNT(*) AS count_no FROM " . _getTableName('catalog_product_entity') . " WHERE sku = ?";
    $count      = $connection->fetchOne($sql, array($sku));
    if($count > 0){
        return true;
    }else{
        return false;
    }
}
function _getIdFromSku($sku){
    $connection = _getConnection('core_read');
    $sql        = "SELECT entity_id FROM " . _getTableName('catalog_product_entity') . " WHERE sku = ?";
    return $connection->fetchOne($sql, array($sku));
}
function _updateStocks($data){
    $connection     = _getConnection('core_write');
    $sku            = $data[0];
    $newQty         = $data[1];
    $productId      = _getIdFromSku($sku);
    $attributeId    = _getAttributeId();
    $sql            = "UPDATE " . _getTableName('cataloginventory_stock_item') . " csi,
                       " . _getTableName('cataloginventory_stock_status') . " css
                       SET
                       csi.qty = ?,
                       csi.is_in_stock = ?,
                       css.qty = ?,
                       css.stock_status = ?
                       WHERE
                       csi.product_id = ?
                       AND csi.product_id = css.product_id";
    $isInStock      = $newQty > 0 ? 1 : 0;
    $stockStatus    = $newQty > 0 ? 1 : 0;
    $connection->query($sql, array($newQty, $isInStock, $newQty, $stockStatus, $productId));
}
$csv                = new Varien_File_Csv();
$csv->setDelimiter(';');
$data               = $csv->getData('supply.csv'); //path to csv
array_shift($data);
$message = '';
$count   = 1;
foreach($data as $_data){
    if(_checkIfSkuExists($_data[0])){
        try{
            _updateStocks($_data);
            $message .= $count . '> Success:: Quantité (' . $_data[1] . ') de la référence (' . $_data[0] . ') a été mis à jour. <br />';
        }catch(Exception $e){
            $message .=  $count .'> Erreur:: Lors de l''update  de la quantité (' . $_data[1] . ') de la référence (' . $_data[0] . ') => '.$e->getMessage().'<br />';
        }
    }else{
        $message .=  $count .'> Erreur:: Produit avec la référence (' . $_data[0] . ') n''existe pas.<br />';
    }
    $count++;
}
echo $message;

我怀疑这个脚本会超过最大执行时间,但是为了回答您的问题,您可以始终跟踪已经处理了多少行,然后在10000行之后停止执行。您还可以使用从$_GET参数传入的偏移量和限制,使其更可控和动态。

您可以拆分CSV或使用array_slice方法从CSV中拆分结果

http://www.php.net/manual/en/function.array-slice.php