简体   繁体   English

Magento CMS阻止集合,无法按商店ID进行过滤-addStoreFilter([storeid])不起作用

[英]Magento CMS Block Collection, can't filter by store id - addStoreFilter([storeid]) not working

How to filter a cms/block (static block) collection ? 如何过滤cms / block(静态块)集合?

This works for cms page: 这适用于cms页面:

$model = Mage::getModel('cms/page');
$collection = $model->getCollection()->addStoreFilter(3);

This does NOT work (returns unfiltered collection): 这不起作用(返回未过滤的集合):

$model = Mage::getModel('cms/block');
$collection = $model->getCollection()->addStoreFilter(3);

I also tried working with the resource models 'cms/block' and 'cms/block_collection', no results. 我还尝试使用资源模型“ cms / block”和“ cms / block_collection”,但没有结果。

Why is Magento that inconsequent ?! 为什么Magento如此罕见?! Sometimes I really start hating Magento for this. 有时候我真的开始为此讨厌Magento。 Please help. 请帮忙。

What do you exactly mean with unfiltered? 您未过滤的确切含义是什么?

The addStoreFilter has a 2nd parameter to include the admin store as well addStoreFilter具有第二个参数,还包括管理存储

addStoreFilter($store, $withAdmin = true)

So if you have any static blocks linked to all stores, these will also be in your collection. 因此,如果您有任何链接到所有商店的静态块,这些也将在您的收藏夹中。

Is this the issue you have? 这是您的问题吗?

Try this code: 试试这个代码:

$collection = Mage::getModel('cms/block')->getCollection();
$select = $collection->getSelect()->join(
    array('block_store' => $collection->getTable('cms/block_store')),
    'main_table.block_id = block_store.block_id',
    array('store_id')
)->where('block_store.store_id IN (?)', array(8));
foreach ($collection as $block) {
   // here you can use $block 
}

You can do the same for cms/page . 您可以对cms/page执行相同的操作。 Just change 只是改变

cms/block -> cms/page and cms/block_store -> cms/page_store cms/block cms/page_store > cms/pagecms/block_store > cms/page_store

OK, first of all thanks to Mike and oleksii.scarychevskyi. 好的,首先要感谢Mike和oleksii.scarychevskyi。 Both answers helped me to get it working. 这两个答案都帮助我使其正常运行。 Using this works perfectly and i prefer this one: 使用此方法效果很好,我更喜欢以下方法:

$collection = Mage::getModel('cms/block')->getCollection()->addStoreFilter(3, false);

oleksiis solution did also work, I didn't know we can change the select in that way. oleksiis解决方案也有效,我不知道我们可以用这种方式更改选择。

This worked for me to get static block by identifier: 这对我来说可以通过标识符获取静态块:

$storeId = 2;
$content = Mage::getModel('cms/block')->setStoreId($storeId)->load('aboutus')->getContent();
echo  $content;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM