网格序列化器在搜索时返回foreach错误

时间:2013-07-11 13:46:55

标签: magento

你好我试图创建一个网格序列化器。单击Admin中的选项卡后,它将正确显示。 但是在网格中我将选择字段从Yes更改为Any以查找所有记录。它给出了Foreach错误(为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;
    }


}

1 个答案:

答案 0 :(得分:0)

您是否检查过$ collection是否为空/有数据?你能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();