1

I have made a custom module for admin in magento. Its namespace is "Assel" and module name is "Character".

This is my config.xml file

<?xml version="1.0"?>
<config>
    <modules>
        <Assel_Character>
            <version>1.0.0</version>
        </Assel_Character>
    </modules>

    <admin>
        <routers>
            <character>
                <use>admin</use>
                <args>
                    <module>Assel_Character</module>
                    <frontName>character</frontName>
                </args>
            </character>
        </routers>
    </admin>

    <adminhtml>
        <menu>
            <cms>
                <children>
                    <character translate="title" module="character">
                        <title>Characters</title>
                        <sort_order>91</sort_order>
                        <action>character/adminhtml_character</action>
                    </character>
                </children>
            </cms>
        </menu>
        <acl>
            <resources>
                <all>
                    <title>Allow Everything</title>
                </all>
                <admin>
                    <children>
                        <character>
                            <title>Character</title>
                        </character>
                    </children>
                </admin>
            </resources>
        </acl>
    </adminhtml>
    <global>
        <models>
            <character>
                <class>Assel_Character_Model</class>
                <resourceModel>character_mysql4</resourceModel>
            </character>
            <character_mysql4>
                <class>Assel_Character_Model_Mysql4</class>
                <entities>
                    <character>
                        <table>character</table>
                    </character>
                </entities>
            </character_mysql4>
        </models>
        <resources>
            <character_setup>
                <setup>
                    <module>Assel_Character</module>
                </setup>
                <connection>
                    <use>core_setup</use>
                </connection>
            </character_setup>
            <character_write>
                <connection>
                    <use>core_write</use>
                </connection>
            </character_write>
            <character_read>
                <connection>
                    <use>core_read</use>
                </connection>
            </character_read>
        </resources>
        <blocks>
            <character>
                <class>Assel_Character_Block</class>
            </character>
        </blocks>
        <helpers>
            <character>
                <class>Assel_Character_Helper</class>
            </character>
        </helpers>
    </global>
</config>

This is my Grid_Container class file named "Character.php" in Assel_Character_Block_Adminhtml folder.

<?php

class Assel_Character_Block_Adminhtml_Character extends Mage_Adminhtml_Block_Widget_Grid_Container
{
    public function __construct()
    {
        $this->_controller = 'adminhtml_character';
        $this->_blockGroup = 'character';
        $this->_headerText = Mage::helper('character')->__('Characters');
        $this->_addButtonLabel = Mage::helper('character')->__('Add New Character');
        parent::__construct();
    }
}

this is my Grid.php file in Assel_Character_Block_Adminhtml_Character folder.

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

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

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

        $this->addColumn('name', array(
            'header'    => Mage::helper('character')->__('Name'),
            'align'     => 'center',
            'index'     => 'name',
        ));

        $this->addColumn('alt_name', array(
            'header'    => Mage::helper('character')->__('Alt. Name'),
            'align'     => 'center',
            'index'     => 'alt_name',
        ));

        $this->addColumn('image', array(
            'header'    => Mage::helper('character')->__('Image'),
            'align'     => 'center',
            'index'     => 'image',
        ));

        $this->addColumn('action',
            array(
                'header'    =>  Mage::helper('character')->__('Action'),
                'width'     => '10',
                'type'      => 'action',
                'getter'    => 'getId',
                'actions'   => array(
                    array(
                        'caption'   => Mage::helper('character')->__('Edit'),
                        'url'       => array('base'=> '*/*/edit'),
                        'field'     => 'id'
                    )
                ),
                'filter'    => false,
                'sortable'  => false,
                'index'     => 'stores',
                'is_system' => true,
        ));

        return parent::_prepareColumns();
    }

    protected function _prepareMassaction() {

        $this->setMassactionIdField('id');
        $this->getMassactionBlock()->setFormFieldName('character');

        $this->getMassactionBlock()->addItem('delete', array(
             'label'    => Mage::helper('character')->__('Delete'),
             'url'      => $this->getUrl('*/*/massDelete'),
             'confirm'  => Mage::helper('character')->__('Are you sure?')
        ));



        array_unshift($statuses, array('label'=>'', 'value'=>''));
        $this->getMassactionBlock()->addItem('status', array(
             'label'=> Mage::helper('character')->__('Change status'),
             'url'  => $this->getUrl('*/*/massStatus', array('_current'=>true)),
             'additional' => array(
                    'visibility' => array(
                         'name' => 'status',
                         'type' => 'select',
                         'class' => 'required-entry',
                         'label' => Mage::helper('character')->__('Status'),
                         'values' => $statuses
                     )
             )
        ));
        return $this;
    }

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

}

This is the my controller file named CharacterController.php in Assel_Character_controllers_Adminhtml folder.

class Assel_Character_Adminhtml_CharacterController extends Mage_Adminhtml_Controller_Action
{

    protected function _initAction() {
        $this->loadLayout()
            ->_setActiveMenu('character')
            ->_addBreadcrumb(Mage::helper('adminhtml')->__('Characters Manager'), Mage::helper('adminhtml')->__('Character Manager'));

        return $this;
    }

    public function indexAction() {
        $this->_initAction()
            ->_addContent($this->getLayout()->createBlock('character/adminhtml_character'))
            ->renderLayout();
    }
}

This is screenshot of my admin. enter image description here

I cann't figure out what is wrong there in code so grid is not showing in backend.

Anyone Please look above code and please tell me where I am wrong..

5
  • you have any admin layout update files for your module ? Dec 2, 2014 at 5:28
  • Hello @programmer_rkt, No I haven't any layout file in my admin layout folder. Also check my updated question. I have mentiond my controller file also. Dec 2, 2014 at 5:35
  • are you sure you are getting your custom collection properly? dump the collection variable $collection = Mage::getModel('character/character')->getCollection(); and make sure it holds a collection Dec 2, 2014 at 5:41
  • You are right. I am getting "bool(false)" on dump $collection variable. How can I solve it.? Dec 2, 2014 at 5:45
  • See my code source bit.ly/dexxtz-custom-grid I made a code simple and functional
    – Dexxtz
    Dec 2, 2014 at 10:31

1 Answer 1

2

Most probably your custom collection for grid returns nothing. ie

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

The protected function _prepareCollection() is used to set a collection for a grid which holds it. Here $collection is empty due to some reason and hence you are setting an empty variable via setCollection() method.

Why Mage::getModel('character/character')->getCollection(); returns an empty collection. This is what you need to resolve your self. However I will give you some hints here.

Here Mage::getModel('character/character') expects a model class Assel_Character_Model_Character to be defined properly at app/code/local/Assel/Character/Model/Character.php. If this class is not defined correctly then it would be the problem here.

Suppose that class is defined correctly. Now you are calling a collection over your model class Mage::getModel('character/character')->getCollection();. Which means you need to define resource for character model and a collection. If you miss or misconfigured any one of these files, then collection would be null.

So what I can suggest you now is, the real problem lies in your module's Model directory. You have misconfigured Character model or its resources. Find it and kill it !!!! You will get output :-)

Good luck

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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