skip to Main Content

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). and i want the following code should fetch all the product which simple_skus_map contains any of the above mentioned word(i.e one/two/three/four)
NOTE: noticed? i have spaces in my custom attribute.

3

Answers


  1. Chosen as BEST ANSWER

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

  2. The query as you supplied can perform slow, as it uses like with wildcards. You can use the find_in_set functionality.

    Luckily magento supports this as well:

    $_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]
        ]
    );
    
    Login or Signup to reply.
  3. I have another solution, with direct SQL queries.
    here is the php code:

    $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| ).

    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”

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search