2012-05-13 6 views
3

私が使用することができます。Magentoの - CMS /ページコレクション - (すなわち、他の店舗に割り当てられていない)指定された店舗IDにユニークなページだけを返すようにフィルタを適用

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

をCMSのコレクションを取得するにはストアIDでフィルタリングされたページ。

しかし、他の店舗にも割り当てられているものを削除するにはどうすればよいですか?

ie:「すべてのストアビュー」をストアビューとして持つアイテムを返さないようにします。 (または、そのCMSページに割り当てられた他の追加のストアID)。そのストアに固有のページのみを返す必要があります。

私は、Store Adminsが他のストアに影響する可能性のあるCMSページと静的ブロックを表示または編集できないように、Aitoc権限モジュールを拡張しています。これには、グリッドからそれらの項目をフィルタリングすることが含まれます。

答えて

1

、私は次のコードで必要なものを達成しました。

私は、Store Adminsが他のストアに影響を与える可能性のあるCMSページと静的ブロックを表示または編集できないように、Aitoc権限モジュールを拡張していました。これはグリッドからそれらの項目をフィルタリングすることを含んでいました。

$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)); 

私はページングとグリッドからフィルタリングするために、2つの異なる方法を使用するために必要な、なぜ私はわかりません。 サイトではmagento 1.5を使用しているため、おそらくそれに関連する問題があります。

どちらの方法でも、この解決策は私のために働いた。

4

クエリー

  1. する必要がありますので、これを行うにはネイティブの収集方法は、特定の店舗に固有のページ

  2. はあなたのフィルタで上記の結果を使用するcms_page_store表は、ありません

私は完全に次のことをテストしていないが、それがない場合には、仕事(と、それは、GIう必要がありますあなたはCMSページの何千を持っている場合は、あなた自身のクエリの良いスタート)

$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()); 
} 

VEのcms/pageコレクションのselectは、集計表データに参加するために変更する価値があるかもしれません。私は読者のための運動としてそれを残しておきます。そのような種類の結合は扱いにくいものです。私自身の面倒を理解した上でアランとヴィタリーによって提案された解決策のいくつかの要素を融合させ

+0

@Vitaly Muminovは以下のように述べています。 –

+0

ありがとうございます - これは全く同じように洞察に満ちた答えです。 – elMarquis

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) 
; 
+0

これは良い解決策のように見えますが、エラーが発生しています: 列が見つかりません:1054不明な列 'stores_count'が 'having clause'にあります。それはそれが上の配列で宣言されているように見えるので、奇妙に見えます。 – elMarquis

+0

おっと、私は間違いをしました。回答に正しいクエリが含まれるようになりました。 –

+0

確認していただきありがとうございます - まだ '' having ''の中に 'Unknown column' stores_count 'があります。 私はMagento 1.5でやっています - それは問題の一部になる可能性があります。 – elMarquis

0

私の解決策は、フィールドstore_idをページ・コレクションにjoinを介して追加し、コレクション・メソッドaddFieldToFilter()を使用することです。

$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]]); 
関連する問題