简体   繁体   English

Magento 1:从集合中的列数据中删除空格

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

i am fetching a product collection and i want to use like condition on custom attribute but the problem is that while using like condition i want to remove all white space contain in my custom attribute value. 我正在获取产品集合,并且想在自定义属性上使用类似条件,但是问题是,在使用类似条件时,我想删除自定义属性值中包含的所有空白。

i have already tried 我已经尝试过

    $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 : (my custom attribute has data like one, two, three ,four). // simple_skus_map :(我的自定义属性的数据如一,二,三,四)。 and i want the following code should fetch all the product which simple_skus_map contains any of the above mentioned word(ie one/two/three/four) NOTE: noticed? 并且我希望以下代码可以获取其中simple_skus_map包含上述任何单词(即一/二/三/四)的所有乘积。注意:注意到了吗? i have spaces in my custom attribute. 我的自定义属性中有空格。

The query as you supplied can perform slow, as it uses like with wildcards. 您提供的查询执行速度可能很慢,因为它与通配符一样使用。 You can use the find_in_set functionality. 您可以使用find_in_set功能。

Luckily magento supports this as well: 幸运的是,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]
    ]
);

I have another solution, with direct SQL queries. 我有直接SQL查询的另一种解决方案。 here is the php code: 这是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);
    }

this is simply, not very high performance, but works. 这很简单,不是很高的性能,但是可以。 Just remember when you write data make sure to set the correct value in the correct fields (you can maybe creating a temporary support table, with |field|id| ). 只要记住在写入数据时,请确保在正确的字段中设置正确的值(也许可以使用| field | id |来创建临时支持表)。

This will remove your blanks from the selected field, and replace them with a non blanks value(or whatever you want to implode them with, just check the implode function). 这将从选定的字段中删除空白,并用非空白值替换它们(或者您要对其进行内插的任何东西,只需检查内插功能)。

"test field blanks" => "testfieldsblanks" “测试字段空白” =>“ testfieldsblanks”

for custom attribute you can do as following, hence it will remove white space from your custom attribute value and match the given/post data 对于自定义属性,您可以执行以下操作,因此它将从自定义属性值中删除空白并匹配给定/发布数据

    $_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