簡體   English   中英

Magento 1:從集合中的列數據中刪除空格

[英]Magento 1 : Removing spaces from column data in collection

我正在獲取產品集合,並且想在自定義屬性上使用類似條件,但是問題是,在使用類似條件時,我想刪除自定義屬性值中包含的所有空白。

我已經嘗試過

    $psku = 'some_sku';
    $_product = Mage::getModel('catalog/product')->getCollection();
    $_product->addFieldToFilter(str_replace(' ', '', 'simple_skus_map'), 
    array(array('like' => '%,'.$psku.',%'),
    array('like' => '%,'.$psku),
    array('like' => $psku.',%'),
    array('like' => $psku)
   ));

// simple_skus_map :(我的自定義屬性的數據如一,二,三,四)。 並且我希望以下代碼可以獲取其中simple_skus_map包含上述任何單詞(即一/二/三/四)的所有乘積。注意:注意到了嗎? 我的自定義屬性中有空格。

您提供的查詢執行速度可能很慢,因為它與通配符一樣使用。 您可以使用find_in_set功能。

幸運的是,magento也支持這一點:

$_product = Mage::getModel('catalog/product')->getCollection();

// Trim spaces, 'save to column' trimmed_simple_skus_map
$_product->addExpressionAttributeToSelect('trimmed_simple_skus_map', 'REPLACE(sku,\' \',\' \')', 'simple_skus_map');

// Find in trimmed set
$_product->addFieldToFilter('trimmed_simple_skus_map', [
        'finset' => [$psku]
    ]
);

我有直接SQL查詢的另一種解決方案。 這是PHP代碼:

$resource = Mage::getSingleton('core/resource');
    $readConnection = $resource->getConnection('core_read');
    $writeConnection = $resource->getConnection('core_write');

    $q_find_blank = "SELECT *FIELD_NAME* FROM simple_skus_map WHERE *FIELD_NAME* LIKE '% %';  ";
    $results = $readConnection->fetchAll($q_find_blank); //get all field with a blank space

    foreach ($results as $result){ //set all the fields in array and removes the blanks
        $result = explode(' ',$result);
        $no_blanks_fields[] = implode("",$result);
    }

    echo print_r($no_blanks_fields);

    foreach ($no_blanks_fields as $no_blanks_field){ //replace them in the database field
        echo $no_blanks_field . "<br>";
        $q_update = "UPDATE simple_skus_map set *FIELD_NAME* = ".$no_blanks_field." WHERE *condition referencing the right field to his value*";
        $writeConnection->query($q_update);
    }

這很簡單,不是很高的性能,但是可以。 只要記住在寫入數據時,請確保在正確的字段中設置正確的值(也許可以使用| field | id |來創建臨時支持表)。

這將從選定的字段中刪除空白,並用非空白值替換它們(或者您要對其進行內插的任何東西,只需檢查內插功能)。

“測試字段空白” =>“ testfieldsblanks”

對於自定義屬性,您可以執行以下操作,因此它將從自定義屬性值中刪除空白並匹配給定/發布數據

    $_product = Mage::getModel('catalog/product')->getCollection();
    $_product->addExpressionAttributeToSelect('trimmed_simple_skus_map', 
    'REPLACE({{simple_skus_map}},\' \',\'\')','simple_skus_map');
    $_product->addFieldToFilter('trimmed_simple_skus_map', [
    'finset' => [$psku]
        ]
    );

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM