Magento attribute filter using "like" does not function properly

I have a problem with addAttributeToFilter() where it does not work as I expect.

 $product = Mage::getModel('catalog/product'); ...snip... $simple_associated_collection = $product->getCollection() ->addAttributeToFilter('type_id', 'simple') ->addAttributeToFilter('sku',array('like'=>$configurable_product_sku.'_%')) ->load(); 

Passing at 4_% I expect to get

 4_1 4_2 

But I also get

 42_1 420_1 

The goal is to get a set of simple products so that I can associate them with their custom parent after import.

How to use LIKE to get the same results as in MySQL ?

+4
source share
2 answers

Magento is not to blame here, _ is a wildcard.

I used Nick's comment for sql echo, which was what I expected.

Fixed

 ->addAttributeToFilter('sku',array('like'=>$configurable_product_sku.'\_%')) 
+6
source

Example

 ->addAttributeToFilter('sku',array('like'=>$configurable_product_sku.'_'.'%')) 
-1
source

Source: https://habr.com/ru/post/1386420/


All Articles