14

I'm getting setSaveParametersInSession() on a non-object

It is on an admin module the path is

Training/Distributor/Block/Adminhtml/ in this folder i have

Distributor/Grid.php and Distributor.php

class Training_Distributor_Block_Adminhtml_Distributor
extends Mage_Adminhtml_Block_Widget_Grid_Container
{

/**
* my understanding:
* $this->_blockGroup . '/' . $this->_controller . '_grid'
* i.e. training_distributor/adminhtml_distributor_grid
*/
public function _construct()
{
    $this->_blockGroup = 'training_distributor';
    $this->_controller = 'adminhtml_distributor';
    $this->_headerText = $this->__('List Distributors');
    $this->_addButtonLabel = $this->__('Add Distributor');

    parent::_construct();
}
}

Why do I get this error?

I can't see any typo's and I'm sure $this->_blockGroup and $this->_controller are right.

Are there any other things I need to look for? or have I missed something?

After a search on google and stack I have found people usually _blockGroup or _controller, but this doesn't seem to be the case for me

===

EDIT

===

<config>
<modules>
    <Training_Distributor>
        <version>0.0.2</version>
    </Training_Distributor>
</modules>
<global>
    <resources>
        <training_distributor_setup>
            <setup>
                <module>Training_Distributor</module>
            </setup>
        </training_distributor_setup>
    </resources>

    <models>
        <training_distributor>
            <class>Training_Distributor_Model</class>
            <resourceModel>training_distributor_resource</resourceModel>
        </training_distributor>

        <training_distributor_resource>
            <class>Training_Distributor_Model_Resource</class>
            <entities>
                <distributor>
                    <table>training_distributor_entity</table>
                </distributor>
            </entities>
        </training_distributor_resource>
    </models>
    <blocks>
        <training_distributor>
            <class>Training_Distributor_Block</class>
        </training_distributor>
    </blocks>
    <helpers>
        <training_distributor>
            <class>Training_Distributor_Helper</class>
        </training_distributor>                
    </helpers>
</global>

<admin>
    <routers>
        <adminhtml>
            <args>
                <modules>
                    <training_distributor before="Mage_Adminhtml">Training_Distributor_Adminhtml</training_distributor>
                </modules>
            </args>
        </adminhtml>
    </routers>
</admin>

<adminhtml>
    <layout>
        <updates>
            <training_distributor>
                <file>training/distributor.xml</file>
            </training_distributor>
        </updates>
    </layout>
</adminhtml>

</config>
  • Add your config.xml file also. – Marius Nov 15 '13 at 12:01
  • Please see edit – tony09uk Nov 15 '13 at 12:06
  • 1
    The config looks ok at a first glance. Try using __construct method instead of _construct. – Marius Nov 15 '13 at 12:13
  • that hasn't worked – tony09uk Nov 15 '13 at 12:49
27

It's hard to say with the information you provided, but here's how I'd debug this.

You didn't mention which file you were having this problem in, but it's probably this part of the base widget grid container that's your problem

#File: app/code/core/Mage/Adminhtml/Block/Widget/Grid/Container.php
protected function _prepareLayout()
{
    $this->setChild( 'grid',
        $this->getLayout()->createBlock( $this->_blockGroup.'/' . $this->_controller . '_grid',
        $this->_controller . '.grid')->setSaveParametersInSession(true) );
    return parent::_prepareLayout();
}

which means Magento's trying to instantiate the block class

createBlock($this->_blockGroup.'/' . $this->_controller . '_grid')

Or in your case

createBlock('training_distributor/adminhtml_distributor_grid')

For some reason, on your system Magento can't instantiate an object from this class alias, which results in the method on a non-object error.

So, step one is adding some debugging code to _prepareLayout

#File: app/code/core/Mage/Adminhtml/Block/Widget/Grid/Container.php
protected function _prepareLayout()
{
    var_dump($this->_blockGroup.'/' . $this->_controller . '_grid');
    ...
}

to make sure the _blockGroup and _controller are set correctly.

Assuming they are, now you're just debugging a normal class lookup. Try running the following code to see what PHP class Magento thinks it should use for your class alias

$class = Mage::getConfig()->getBlockClassName('training_distributor/adminhtml_distributor_grid')
var_dump($class);

Glancing at your config, this should be something like

Training_Distributor_Block_Adminhtml_Distributor_Grid

If $class is empty or false, it means you have an error in your config.xml file. You can debug this by jumping to the getGroupedClassName method.

public function getGroupedClassName($groupType, $classId, $groupRootNode=null)
{
    ...
}

This is the method where Magento references the merged config and pulls out a class name.

Finally, if $class does contain a class name, it means Magento's autoloader can't find your class. Try instantiating this class directly (again, as a debugging exercise)

$o = new Training_Distributor_Block_Adminhtml_Distributor_Grid;
var_dump($o);

You'll probably get some sort of class not found error, either because

  1. Your class isn't in the proper location (app/code/[codepool]/Training/Distributor/Block/Adminhtml/Distributor/Grid.php

  2. The class defined in Grid.php has a typo in it

1

The $this->_blockGroup is a very confusing name but it should be the folder name and not the module. In you case I think it should be $this->_blockGroup = 'distributor';

If you look at Mage_Currencysymbol_Block_Adminhtml_System_Currencysymbol as an example the blockGroup is the folder name and not the module name in the config.xml

// Mage_Currencysymbol_Block_Adminhtml_System_Currencysymbol
$this->_blockGroup = 'currencysymbol_system';
  • Thanks for your suggestion, I have tried it but was unsuccessful. – tony09uk Nov 15 '13 at 13:19
  • did you try distributor_distributor? – David Manners Nov 15 '13 at 13:21
  • After the example you suggested I have tried 'training_distributor', 'distributor', 'distributor_distributor', 'training_distributor_distributor' – tony09uk Nov 15 '13 at 13:29
  • @DavidManners +1 for your it works for me...thanks... – Keyur Shah Jan 30 '14 at 8:21
0

Check your folder structure one more time to make sure that all the files are located at correct path.

In my case for this issue the file was placed at wrong location. I placed the Grid.php in controller/Adminhtml folder rather that Block/Adminhtml/Grid.php

  • Naming Conventions are very important. So while naming your namespace and modules make sure that they are correct and according to magento standards... – devJsha Feb 19 '14 at 6:59
0

Check your path to distributor.xml file. It should be placed in app/design/adminhtml/default/default/layout/distributor.xml.

In the code related to config.xml that you posted show that

<adminhtml>
    <layout>
        <updates>
            <training_distributor>
                <file>training/distributor.xml</file>
            </training_distributor>
        </updates>
    </layout>
</adminhtml>

rather than this it should be like this

<adminhtml>
    <layout>
        <updates>
            <training_distributor>
                <file>distributor.xml</file>
            </training_distributor>
        </updates>
    </layout>
</adminhtml>

and the file should be placed at app/design/adminhtml/default/default/layout/distributor.xml

Try doing this if it is not your present scenario.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.