I am currently developing a banner manager module that allows the admin to upload banners and select which store views those banners will appear on. I have everything working fine when a single store view is selected using a multi select attribute, however when you select multiple store views Magento seems to look for the selected store ID's as a string rather than individually.
For example for a banner I have 3 store views selected. Magento returns '1,3,4' as the value. Rather than looking for each number individually Magento looks for the string'1,3,4'. See the function I am using below:
function getHomePageBanners() {
$storeId = Mage::app()->getStore()->getStoreId();
$customerId = Mage::getSingleton('customer/session')->getCustomerGroupId();
$collection = Mage::getModel("homepagebanner/homepagebanner")->getCollection()
->addFieldToFilter('status', array('eq' => 0))
->addFieldToFilter('store_view_id', array('in' => array(0,$storeId)))
->addFieldToFilter('customer_group_id', array('in' => array($customerId)))
->setOrder('position', 'ASC')
->toArray();
return $collection['items'];
}
The same applies when I am filtering via customer group as well.
Would anyone know how I can get Magento to check each value individually in the string rather than as a whole?
->addFieldToFilter('store_view_id', array('finset' => array(0,$storeId)))
instead of->addFieldToFilter('store_view_id', array('in' => array(0,$storeId)))
but I'm not surefinset
works with arrays. It should work for a single value. As a recomandation you should use a separate table to store the relation between banners and stores. It's cleaner and you can add constraints to it, so when you delete a store the relations will be automatically deleted.