Magento - cms / page collection - apply a filter to return only pages that are unique to a given store identifier (i.e. also are not assigned to other stores)

I can use:

Mage::getModel('cms/page')->getCollection()->addStoreFilter($store_id);

to get a collection of CMS pages filtered by store ID.

But how can I remove it, which are also assigned to other stores?

those. I don’t want it to return items that have "All store views" in the form of storage. (Or any other additional repository identifier assigned to this CMS page.) It should only return pages that are unique to this repository.

I am expanding the Aitoc permissions module so store administrators cannot view or edit CMS pages and static blocks that may affect other stores. This includes filtering these items from the grid.

+3
source share
4 answers

Alloying some elements of the solutions proposed by Alan and Vitaly with my own cumbersome understanding, I achieved what I need with the following code.

To include context, I expanded the Aitoc permissions module so that store administrators cannot view or edit CMS pages and static blocks that could affect other stores. This included filtering these items from the grid.

$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, , , , .

, .

+1

,

  • cms_page_store , .

, ( , )

$page     = Mage::getModel('cms/page');
$resource = $page->getResource();
$read     = $resource->getReadConnection();

#$select   = $read->query('SELECT page_id FROM ' . $resource->getTable('cms/page_store') . ' GROUP BY store_id');

//set total count to look for.  1 means the page only appears once.
$total_stores_count_to_look_for = '1';

//get the table name.  Need to pass through getTable to ensure any prefix used is added
$table_name                     = $resource->getTable('cms/page_store');

//aggregate count select from the cmd_page_store database
//greater than 0 ensures the "all stores" pages aren't selected
$select = $read->query('SELECT page_id as total
FROM '.$table_name.' 
WHERE store_id > 0 
GROUP BY page_id
HAVING count(page_id) = ?',array($total_stores_count_to_look_for));

//fetch all the rows, which will be page ids
$ids   = $select->fetchAll(); 

//query for pages using IDs from above
$pages = Mage::getModel('cms/page')->getCollection()->addFieldToFilter('page_id',array('in'=>$ids));

foreach($pages as $page)
{
    var_dump($page->getData());
}

CMS, , cms/page select . , .

+4
$collection = Mage::getModel('cms/page')->getCollection();
$collection->getSelect()
    ->join(
        array('cps' => $collection->getTable('cms/page_store')),
        'cps.page_id = main_table.page_id AND cps.store_id != 0',
        array('store_id')
    )
    ->columns(array('stores_count' => new Zend_Db_Expr('COUNT(cps.store_id)')))
    ->group('main_table.page_id')
    ->having('stores_count = ?', 1)
    ->having('cps.store_id = ?', $storeId)
;
+4
source

My solution is to add the store_id field to the page collection via the connection and use the addFieldToFilter () collection method.

$pages = Mage::getModel('cms/page')->getCollection();

$pages->getSelect()->joinInner(
     array('cms_page_store' => 'cms_page_store'),
     'main_table.page_id = cms_page_store.page_id',
     array()
);

$pages->addFieldToFilter('store_id', ['in' => [1, 2]]);
0
source

All Articles