cancel
Showing results for 
Search instead for 
Did you mean: 

Magento 1: addfieldtofilter pass a string of integers to WHERE `IN` clause?

SOLVED

Magento 1: addfieldtofilter pass a string of integers to WHERE `IN` clause?

I have created a custom module and trying to get the collection with model. I am passing following filter to collection. Which is not working at my end and replying wrong results.

 

Here is my code:

 

For fetching all enabled tags:

 

$tagData = Mage::getModel('homefeatures/hometags')->getCollection(); 
$tagData->addFieldToFilter('tag_status', array("eq" => 1));
foreach($tagData as $tagcollection) {
$tag_name = $tagcollection->getTagName();
$tag_id = $tagcollection->getId();
$tagIds .= $tag_id.', ';
}

For getting collection of posts with enabled tags:

 

$values = rtrim($tagIds, ', ');
//echo $values; // it return me 5, 6, 7, 8, 9, 10 ie: id's of tags are enable
$item_collection = Mage::getModel('homefeatures/items')->getCollection();
$item_collection->addFieldToFilter('items_tags',array("in" => array($values)));
$item_collection->addFieldToFilter('status', array("eq" => 1));
$item_collection->getSelect()->limit(12);
// output of above code is: it return nothing to me, showing blank page

Problem is with passing integer values to WHERE IN clause. If I checked the query by print it's automatically adding a single quote at IN clause(''). Which is causing problem here:

 

SELECT `main_table`.* FROM `grazitti_items` AS `main_table` WHERE (items_tags IN('5, 6, 7, 8, 9, 10')) AND (status = 1) LIMIT 12

 

My query should be like this:

 

SELECT `main_table`.* FROM `grazitti_items` AS `main_table` WHERE (items_tags IN(5, 6, 7, 8, 9, 10)) AND (status = 1) LIMIT 12

 

Do you guys having any solution how can I pass a string of integers to WHERE `IN` clause? 

 

 

1 ACCEPTED SOLUTION

Accepted Solutions

Re: Magento 1: addfieldtofilter pass a string of integers to WHERE `IN` clause?

Here is the solution I found working for me. Adding here for helping people.

 

$tagData = Mage::getModel('homefeatures/hometags')->getCollection(); 
$tagData->addFieldToFilter('tag_status', array("eq" => 1));
foreach($tagData as $tagcollection) {
	$tag_name = $tagcollection->getTagName();
	$tag_id = $tagcollection->getId();
	$tagIds .= $tag_id.', ';
}

$values = rtrim($tagIds, ', ');
$valuesArray = explode(', ', $values);
$filters = array();
foreach($valuesArray as $inrt){
	$filters[] = array('finset'=> array($inrt));
}

$item_collection = Mage::getModel('homefeatures/items')->getCollection();
$item_collection_priority->addFieldToFilter('items_tags',array($filters));
$item_collection->addFieldToFilter('status', array("eq" => 1));
$item_collection->getSelect()->limit(12);

I replaced `in` with `finset` and created filters.

View solution in original post

3 REPLIES 3

Re: Magento 1: addfieldtofilter pass a string of integers to WHERE `IN` clause?

Hi @PankajS_Magento 

 

You need to explode your string as array. Try the following way:

$values = rtrim($tagIds, ', ');
$values = explode(',', $values);
$item_collection = Mage::getModel('homefeatures/items')->getCollection();
$item_collection->addFieldToFilter('items_tags',array("in" => $values));
$item_collection->addFieldToFilter('status', array("eq" => 1));
$item_collection->getSelect()->limit(12);

 

 

-----
If Issue Solved, Click Kudos and Accept As solutions.
Sohel Rana, 7x Magento 2, 2x Magento 1 Certified

Re: Magento 1: addfieldtofilter pass a string of integers to WHERE `IN` clause?

@Sohel 

 

Thanks for your reply but its not working for me.

Re: Magento 1: addfieldtofilter pass a string of integers to WHERE `IN` clause?

Here is the solution I found working for me. Adding here for helping people.

 

$tagData = Mage::getModel('homefeatures/hometags')->getCollection(); 
$tagData->addFieldToFilter('tag_status', array("eq" => 1));
foreach($tagData as $tagcollection) {
	$tag_name = $tagcollection->getTagName();
	$tag_id = $tagcollection->getId();
	$tagIds .= $tag_id.', ';
}

$values = rtrim($tagIds, ', ');
$valuesArray = explode(', ', $values);
$filters = array();
foreach($valuesArray as $inrt){
	$filters[] = array('finset'=> array($inrt));
}

$item_collection = Mage::getModel('homefeatures/items')->getCollection();
$item_collection_priority->addFieldToFilter('items_tags',array($filters));
$item_collection->addFieldToFilter('status', array("eq" => 1));
$item_collection->getSelect()->limit(12);

I replaced `in` with `finset` and created filters.