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?
Solved! Go to Solution.
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.
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);
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.