5

I have a field inside a table that contains a string made of the country codes:IT,GB,US,DU,... and I need to check if the customer's country (GB,IT,..) is inside this string.

I know I can use CHARINDEX() in SQL to accomplish this result or retrieve the string, convert it in an array and then check with in_array, but I wonder if there is a way (and if it is better) to do this task during the query.

This is my current code:

Mage::getModel('razorphyn_country/product')->getCollection()
 ->addFieldToFilter('active', 1);
 ->addFieldToFilter('productId', $productId);

And country is the field that contains the string

7ochem
7,61516 gold badges54 silver badges82 bronze badges
asked May 18, 2015 at 6:52

1 Answer 1

12

let's say that the country id you are looking for is $countryId.
And the field name is country_id.
You can add a filter like this:

->addFieldToFilter('country_id', array('finset' => $countryId));

Below the surface this will convert into the use of MySQL string function FIND_IN_SET(str, strlist).

7ochem
7,61516 gold badges54 silver badges82 bronze badges
answered May 18, 2015 at 6:57
0

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.