Magento 1.9 Error – Fatal Error: Call to a Member Function setSaveParametersInSession() on a Non-Object

gridmagento-1.9module

I am facing bellow error in my custom module:

Fatal error: Call to a member function setSaveParametersInSession()
on a non-object in
app\code\core\Mage\Adminhtml\Block\Widget\Grid\Container.php on line 66

Below I am providing you the relevant files.

Controller

<?php
class Primasource_Sidebar_Adminhtml_JewelryController extends Mage_Adminhtml_Controller_Action
{

    public function indexAction()
    {

        $this->loadLayout();
        $this->renderLayout();
    }

    public function newAction()
    {
        $this->_forward('edit');
    }

    public function editAction()
    {
        $id = $this->getRequest()->getParam('id', null);
        $model = Mage::getModel('sidebar/sidebar1');
        if ($id) {
            $model->load((int) $id);
            if ($model->getId()) {
                $data = Mage::getSingleton('adminhtml/session')->getFormData(true);
                if ($data) {
                    $model->setData($data)->setId($id);
                }
            } else {
                Mage::getSingleton('adminhtml/session')->addError(Mage::helper('sidebar')->__('Link does not exist'));
                $this->_redirect('*/*/');
            }
        }
        Mage::register('example_data', $model);

        $this->loadLayout();
        $this->getLayout()->getBlock('head')->setCanLoadExtJs(true);
        $this->renderLayout();
    }

    public function saveAction()
    {
        if ($data = $this->getRequest()->getPost())
        {
            $model = Mage::getModel('sidebar/sidebar1');
            $id = $this->getRequest()->getParam('id');
            if ($id) {
                $model->load($id);
            }
            $model->setData($data);

            Mage::getSingleton('adminhtml/session')->setFormData($data);
            try {
                if ($id) {
                    $model->setId($id);
                }
                $model->save();

                if (!$model->getId()) {
                    Mage::throwException(Mage::helper('sidebar')->__('Error saving Links'));
                }

                Mage::getSingleton('adminhtml/session')->addSuccess(Mage::helper('sidebar')->__('Links were successfully saved.'));
                Mage::getSingleton('adminhtml/session')->setFormData(false);

                // The following line decides if it is a "save" or "save and continue"
                if ($this->getRequest()->getParam('back')) {
                    $this->_redirect('*/*/edit', array('id' => $model->getId()));
                } else {
                    $this->_redirect('*/*/');
                }

            } catch (Exception $e) {
                Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
                if ($model && $model->getId()) {
                    $this->_redirect('*/*/edit', array('id' => $model->getId()));
                } else {
                    $this->_redirect('*/*/');
                }
            }

            return;
        }
        Mage::getSingleton('adminhtml/session')->addError(Mage::helper('sidebar')->__('No data found to save'));
        $this->_redirect('*/*/');
    }

    public function deleteAction()
    {
        if ($id = $this->getRequest()->getParam('id')) {
            try {
                $model = Mage::getModel('sidebar/sidebar1');
                $model->setId($id);
                $model->delete();
                Mage::getSingleton('adminhtml/session')->addSuccess(Mage::helper('sidebar')->__('The Links Have been deleted.'));
                $this->_redirect('*/*/');
                return;
            }
            catch (Exception $e) {
                Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
                $this->_redirect('*/*/edit', array('id' => $this->getRequest()->getParam('id')));
                return;
            }
        }
        Mage::getSingleton('adminhtml/session')->addError(Mage::helper('adminhtml')->__('Unable to find the Links to delete.'));
        $this->_redirect('*/*/');
    }

}

Block

I have created jewelery block

<?php
class Primasource_Sidebar_Block_Adminhtml_Jewelry extends Mage_Adminhtml_Block_Widget_Grid_Container
{
    protected $_addButtonLabel = 'Add New Links';

    public function __construct()
    {
        $this->_blockGroup = 'sidebar';

        $this->_controller = 'adminhtml_jewelry';

        $this->_headerText = Mage::helper('sidebar')->__('sidebar');

        parent::__construct();

    }
}
Grid is at Primasource/Sidebar/Block/Adminhtml/Sidebar/Gridjewelry.php



       <?php
 class Primasource_Sidebar_Block_Adminhtml_Sidebar_Gridjewelry extends Mage_Adminhtml_Block_Widget_Grid
{
    public function __construct()
    {
        parent::__construct();
        $this->setId('sidebar_gridjewelry');
        $this->setDefaultSort('id');
        $this->setDefaultDir('asc');
        $this->setSaveParametersInSession(true);
    }

    protected function _prepareCollection()
    {
        $collection = Mage::getModel('sidebar/sidebar1')->getCollection();
        $this->setCollection($collection);
        return parent::_prepareCollection();
    }

    protected function _prepareColumns()
    {
        $this->addColumn('id', array(
            'header'    => Mage::helper('sidebar1')->__('ID'),
            'align'     =>'right',
            'width'     => '50px',
            'index'     => 'id',
        ));

        $this->addColumn('links', array(
            'header'    => Mage::helper('sidebar1')->__('Links'),
            'align'     =>'left',
            'index'     => 'links',
        ));

        $this->addColumn('links_url', array(
            'header'    => Mage::helper('sidebar1')->__('Url-Links'),
            'align'     =>'left',
            'index'     => 'links_url',
        ));

        /*$this->addColumn('other', array(
            'header'    => Mage::helper('sidebar')->__('Other'),
            'align'     => 'left',
            'index'     => 'other',
        ));*/

        return parent::_prepareColumns();
    }

    public function getRowUrl($row)
    {
        return $this->getUrl('*/*/edit', array('id' => $row->getId()));
    }
}

Please tell me anyone where I am wrong.

Best Answer

Let us look what happend here. If you make a look on the line that is expressed by the error, you can see this code

protected function _prepareLayout()
{
    $this->setChild( 'grid',
        $this->getLayout()->createBlock( $this->_blockGroup.'/' . $this->_controller . '_grid',
        $this->_controller . '.grid')->setSaveParametersInSession(true) );
    return parent::_prepareLayout();
}

As you can see, setSaveParametersInSession() function is used inside _prepareLayout() method. This method will invoke by magento, when it starts to prepare layout to render. Here you can see that admin grid container (ie Mage_Adminhtml_Block_Widget_Grid_Container class) is trying to set a child block. If you make a good look there, you can see that, it uses some properties that we are set through custom grid container(ie Afycon_Customercredits_Block_Adminhtml_Credits class) in order to generate the child block. In custom grid container, we are setting these two properties

$this->_controller = 'customercredits_credits';
$this->_blockGroup = 'customercredits';

Using these values, magento first try to generate a block and then set that block as child to grid container. But what happens here is, Magento can't generate a valid block instance due to wrong property values. According to the given value, Magento now trying to create a block customercredits/customercredits_credits_grid( this stands for Afycon_Customercredits_Block_CustomerCredits_Credits_Grid) and it cannot find that block instance defined anywhere. So in effect, Magento cannot set grid block and as a result it throws an error.

So here your grid block is Afycon_Customercredits_Block_Adminhtml_Credits_Grid. In order to autogenerate this class, you need to set

$this->_controller = 'adminhtml_credits';

BIG NOTE

"$this->_blockGroup should hold block alias name that you set through config.xml and $this->_controller should hold the path to your grid container".

Hope that helps

Related Topic