从数据库中获取所有值,并通过它们获取foreach

时间:2013-06-20 17:43:35

标签: php sql magento

我正在使用脚本来更新价格。到目前为止,它从CSV获取数据,并使用它在数据库中查找并使用该行的CSV信息更新产品。

我遇到的问题是CSV中的SKU与Magento中的SKU不完全匹配。例如,在CSV中可能有1234但是在magento中,需要使用此行中的信息进行更新的产品将是001234_01001234_02以及001234_03等等

所以我添加了一个LIKE搜索来获取Magento的所有结果,就像CSV中该行的SKU一样。

问题是它只找到一个更新它然后移动到下一行。因此它会更新001234_01,但我需要它来更新001234_02001234_03,然后再转到下一行。

我想我需要将_updatePrices调整为foreach,但我似乎无法让它工作。

$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','1024M');

    /***************** UTILITY FUNCTIONS ********************/
    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 _getIdFromSku($sku){
        $connection = _getConnection('core_read');
        $sql        = "SELECT entity_id FROM " . _getTableName('catalog_product_entity') . " WHERE sku LIKE '%$sku%'";
        return $connection->fetchOne($sql, array($sku));

    }

    function _checkIfSkuExists($sku){
        $connection = _getConnection('core_read');
        $sql        = "SELECT * FROM " . _getTableName('catalog_product_entity') . " WHERE sku LIKE '%$sku%'";
        // I think you can try $conn->fetchAll();
        return $connection->fetchAll($sql, array($sku));
        // print_r this - it will be an array (empty or not)
        print_r($connection);
    }

    function _updatePrices($data){
        $connection     = _getConnection('core_write');
        $sku            = $data[0];
        $newPrice       = $data[4];
        $specialPrice   = $data[6];
        $status         = $data[10];
        $productId      = $row['entity_id'];
        $attributeId    = _getAttributeId();

        $sql = "UPDATE " . _getTableName('catalog_product_entity_decimal') . " cped
                    SET  cped.value = ?
                WHERE  cped.attribute_id = ?
                AND cped.entity_id = ?";
        $connection->query($sql, array($newPrice, $attributeId, $productId));

        $sql2 = "UPDATE " . _getTableName('catalog_product_entity_decimal') . " cped
                    SET  cped.value = ?
                WHERE  cped.attribute_id = ?
                AND cped.entity_id = ?";
        $connection->query($sql2, array($specialPrice, '567', $productId));

        if ($status == "A") {
            $sql3 = "UPDATE " . _getTableName('catalog_product_entity_int') . " cped
                    SET  cped.value = ?
                WHERE  cped.attribute_id = ?
                AND cped.entity_id = ?";
        $connection->query($sql3, array('1', '273', $productId));
        } else {
           $sql4 = "UPDATE " . _getTableName('catalog_product_entity_int') . " cped
                    SET  cped.value = ?
                WHERE  cped.attribute_id = ?
                AND cped.entity_id = ?";
        $connection->query($sql4, array('2', '273', $productId));
        }

    }
    /***************** UTILITY FUNCTIONS ********************/

    // Here comes your code after UTILITY FUNCTIONS:
    $csv                = new Varien_File_Csv();
    $data               = $csv->getData('price-update/PRDAH014.csv'); //path to csv
    array_shift($data);

    $message = '';
    $count   = 1;
    foreach($data as $_data){
        // insted of checking if there exists nuber of rows         
        // if(_checkIfSkuExists($_data[0])){
        // you get rows that match your LIKE query
        $skuRows = _checkIfSkuExists($_data[0]);

        if(0 < sizeof($skuRows)){
            // okay you have records, let's iterate through them
            foreach ($skuRows as $row) {
                try{


                    // modify your _updatePrices so as to use record ID from $row and CSV data from $data both
                    _updatePrices($_data, $row);

                } catch(Exception $e){
                    // exception warning
                }
            }
        }else{
            // no records warning
        }
    } // foreach($data as $_data) ends

1 个答案:

答案 0 :(得分:1)

好的,您的函数_checkIfSkuExists($sku)应该返回不计行,而是返回行:

function _checkIfSkuExists($sku){
    $connection = _getConnection('core_read');
    $sql        = "SELECT * FROM " . _getTableName('catalog_product_entity') . " WHERE sku LIKE '%$sku%'";

    // I think you can try $conn->fetchAll();
    return $connection->fetchAll($sql, array($sku));
    // print_r this - it will be an array (empty or not)
}
// in each row's data there's a record (or row) ID

function _updatePrices($data, $row){
    $connection     = _getConnection('core_write');
    $sku            = $data[0];
    $newPrice       = $data[4];
    $specialPrice   = $data[6];
    $status         = $data[10];
    // here - you don't need to find `entity_id`, you already have it in $row
    //$productId      = _getIdFromSku($sku);
    $productId        = $row['entity_id'];
    $attributeId    = _getAttributeId();

   // all your code remains the same
   // ....
} // function _updatePrices ends

// ....

// Here comes your code after UTILITY FUNCTIONS:
$csv                = new Varien_File_Csv();
$data               = $csv->getData('price-update/PRDAH014.csv'); //path to csv
array_shift($data);

$message = '';
$count   = 1;
foreach($data as $_data){
    // insted of checking if number of rows that 
    // match your LIKE query is greater than zero
    // if(_checkIfSkuExists($_data[0])){
    // you get rows themselves that match your LIKE query
    $skuRows = _checkIfSkuExists($_data[0]);
    if(0 < sizeof($skuRows)){
        // okay you have records, let's iterate through them
        foreach ($skuRows as $row) {
            try{
                // modify your _updatePrices so as to use record ID from $row and CSV data from $data both
                _updatePrices($_data, $row);

            } catch(Exception $e){
                // exception warning
            }
        }
    }else{
        // no records warning
    }
    $count++;
} // foreach($data as $_data) ends
// ... 

我对Magento并不熟悉,但我认为algorythm是这样的。