Filter Collection by Multiple Values from Multiselect Attribute in Magento

backendcollection-filteringcollection;multistore

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?

Best Answer

I managed to get this to work using Marius advice and using the answer from the post below:

Collection with FIND_IN_SET on multiselect attributes, but some has only one value

My code now reads

    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(
                    array('finset'=> array('0')),
                    array('finset'=> array($storeId)),
                )
            )
            ->addFieldToFilter('customer_group_id',array('finset'=>array($customerId)))
            ->setOrder('position', 'ASC')
            ->toArray();

    return $collection['items'];
}