将 Alan 和 Vitaly 提出的解决方案的一些元素与我自己的繁琐理解融合在一起,我通过以下代码实现了我所需要的。
具体来说,我正在扩展 Aitoc 权限模块,以便商店管理员无法查看或编辑可能影响其他商店的 CMS 页面和静态块。这涉及从网格中过滤这些项目。
$collection = Mage::getModel('cms/page')->getCollection();
$collection->addStoreFilter(Mage::helper('aitpermissions')->getStoreIds());
$conn = Mage::getSingleton('core/resource')->getConnection('core_read');
$page_ids = array();
foreach($collection as $key=>$item) {
$page_id = $item->getId();
$results = $conn->fetchAll("SELECT * FROM cms_page_store
WHERE page_id = ".$page_id.";");
$count = 0;
$arr_stores = array();
foreach($results as $row) {
$arr_stores[] = $row['store_id'];
$count++;
}
//We dont want to show the item if any of the following are true:
//The store id = 0 (Means its assigned to All Stores)
//There is more than one store assigned to this CMS page
if( in_array('0',$arr_stores) || $count>1) {
//This removes results from the grid (but oddly not the paging)
$collection->removeItemByKey($key);
}
else {
//build an array which we will use to remove results from the paging
$page_ids[] = $page_id;
}
}
//This removes results from paging (but not the grid)
$collection->addFieldToFilter('page_id',array('in'=>$page_ids));
我不确定为什么需要使用两种不同的方法从分页和网格中过滤。该站点使用magento 1.5,因此可能存在与此相关的问题。
无论哪种方式,这个解决方案都对我有用。