2013-07-11 35 views
0

你好我試圖創建一個網格序列化器。點擊管理員中的標籤後,它會正確顯示。 但是,在網格中,我將選擇字段從是更改爲任何以查找所有記錄。它給出了Foreach錯誤(爲foreach提供的無效參數)。Grid序列化器在搜索時回調foreach錯誤

佈局:

 <manager_adminhtml_manager_category> 
     <block type="core/text_list" name="root" output="toHtml"> 
      <block type="manager/adminhtml_manager_edit_tab_category" name="categories.grid"/> 
      <block type="adminhtml/widget_grid_serializer" name="grid_serializer"> 
       <reference name="grid_serializer"> 
        <action method="initSerializerBlock"> 
         <grid_block_name>categories.grid</grid_block_name> 
         <data_callback>getSelectedCategories</data_callback> 
         <hidden_input_name>links[category]</hidden_input_name> 
         <reload_param_name>category</reload_param_name> 
        </action> 
        <action method="addColumnInputName"> 
         <input_name>position</input_name> 
        </action> 
       </reference> 
      </block> 
     </block> 
    </manager_adminhtml_manager_category> 

    <manager_adminhtml_manager_categorygrid> 
      <block type="core/text_list" name="root" output="toHtml"> 
       <block type="manager/adminhtml_manager_edit_tab_category" name="categories.grid"/> 
      </block> 
    </manager_adminhtml_manager_categorygrid> 

控制器

class Excellence_Manager_Adminhtml_ManagerController extends Mage_Adminhtml_Controller_action 
{ 



     public function categoryAction(){ 
       $this->loadLayout(); 
       $this->getLayout()->getBlock('categories.grid')->setCategory($this->getRequest()->getPost('category',null)); 

       $this->renderLayout(); 
     } 



    public function categorygridAction(){ 
     $this->loadLayout(); 
     $this->getLayout()->getBlock('categories.grid') 
     ->setCategory($this->getRequest()->getPost('category', null)); 
     $this->renderLayout(); 
    } 

<?php 
class Excellence_Manager_Block_Adminhtml_Manager_Edit_Tab_Category extends Mage_Adminhtml_Block_Widget_Grid 
{ 
    public function __construct() 
    { 
     parent::__construct(); 
     $this->setId('categoryGrid'); 
     $this->setUseAjax(true); // Using ajax grid is important 
     $this->setDefaultSort('entity_id'); // default sort from the _prepareColoum below 
     $this->setDefaultFilter(array('in_producted'=>1)); // By default we have added a filter for the rows, that in_products value to be 1 
     $this->setSaveParametersInSession(false); 
    } 

    protected function _prepareCollection() 
    { 
     $collection = Mage::getResourceModel('catalog/category_collection'); 


     $tm_id = $this->getRequest()->getParam('id'); 
     if(!isset($tm_id)) { 
      $tm_id = 0; 
     } 
     Mage::getResourceModel('manager/cat')->addGridPosition($collection,$tm_id); 

     $this->setCollection($collection); 
     return parent::_prepareCollection(); 
    } 




    protected function _addColumnFilterToCollection($column) 
    { 
     // Set custom filter for in product flag 
     if ($column->getId() == 'in_producted') { 
      $ids = $this->_getSelectedCategories(); 
      if (empty($ids)) { 
       $ids = 0; 
      } 
      if ($column->getFilter()->getValue()) { 
       $this->getCollection()->addFieldToFilter('entity_id', array('in'=>$ids)); 
      } else { 
       if($ids) { 
        $this->getCollection()->addFieldToFilter('entity_id', array('nin'=>$ids)); 
       } 
      } 
     } else { 
      parent::_addColumnFilterToCollection($column); 
     } 
     return $this; 
    } 

    protected function _prepareColumns() 
    { 

      $this->addColumn('in_producted', array(
       'header_css_class' => 'a-center', 
       'type'    => 'checkbox', 
       'name'    => 'customer', 
       'values'   => $this->_getSelectedCategories(), 
       'align'    => 'center', 
       'index'    => 'entity_id' 
      )); 
      $this->addColumn('entity_id', array(
      'header' => Mage::helper('customer')->__('ID'), 
      'width'  => '50px', 
      'index'  => 'entity_id', 
      'type' => 'number', 
      )); 
      $this->addColumn('name', array(
      'header' => Mage::helper('customer')->__('Name'), 
      'index'  => 'name' 
      )); 
      $this->addColumn('is_active', array(
      'header' => Mage::helper('customer')->__('Status'), 
      'width'  => '150', 
      'index'  => 'is_active' 
      )); 


      return parent::_prepareColumns(); 
    } 

    protected function _getSelectedCategories() // Used in grid to return selected customers values. 
    { 
     $categories = array_keys($this->getSelectedCategories()); 
     return $categories; 
    } 

    public function getGridUrl() 
    { 
     return $this->_getData('grid_url') ? $this->_getData('grid_url') : $this->getUrl('*/*/categorygrid', array('_current'=>true)); 
    } 
    public function getSelectedCategories() 
    { 
     // Categories Data 
     $tm_id = $this->getRequest()->getParam('id'); 

     if(!isset($tm_id)) { 
      $tm_id = 0; 
     } 
     $collection = Mage::getModel('manager/cat')->getCollection(); 
     $collection->addFieldToFilter('manager_id',$tm_id); 
     $catIds = array(); 
     foreach($collection as $obj){ 
      $catIds[$obj->getCategoryId()] = array('position'=>$obj->getPosition()); 
     } 
     return $catIds; 
    } 


} 
+0

好的我發現它,但它很奇怪。如果在_preparCollection中使用$ collection = Mage :: getModel('catalog/product') - > getCollection();它顯示了所有的產品,但如果我做$ collection = Mage :: getModel('catalog/category') - > getCollection();然後我得到foreach錯誤。我怎樣才能讓所有類別進入收藏? – RIK

回答

0

你檢查了$集合不爲空/有數據?你可以var_dump($ collection);在你的foreach循環之前併發布輸出?

通過更換試試這個:

$collection = Mage::getModel('manager/cat')->getCollection(); 
$collection->addFieldToFilter('manager_id',$tm_id); 

$collection = Mage::getModel('manager/cat')->getCollection()->addFieldToSelect('*')->addFieldToFilter('manager_id',$tm_id); 

如果不行試試你的foreach循環之前加入$collection->load();

+0

我試着在getSelectedCustomers()中的集合上做var_dump,但在向我展示任何結果之前它已經打破。 – RIK

+0

在categoryAction()我使用$ this-> getRequest() - > getPost('category',null)。你能告訴我,這個「類別」變量是否被正確定義?這是來自佈局嗎? – RIK

+0

好的,我發現它,但它很奇怪。如果在_preparCollection中使用$ collection = Mage :: getModel('catalog/product') - > getCollection();它顯示了所有的產品,但如果我做$ collection = Mage :: getModel('catalog/category') - > getCollection();然後我得到foreach錯誤。我怎樣才能讓所有類別進入收藏? – RIK