Call-back icon  Sales: Call 877.832.5289 (N America)|310.295.4144 (International)

Magento

eCommerce Software for Online Growth

Magento Forum

   
Page 2 of 14
Creating a Module with custom Database Table Tutorial
 
alistek
Sr. Member
 
Total Posts:  293
Joined:  2008-04-02
Normal, IL
 

/app/code/local/LC/News/Helper/Data.php

<?php

class LC_News_Helper_Data extends Mage_Core_Helper_Abstract
{

}

?>

At this point, all this does is create a menu item called News and a sub-menu item called Manage News.  If you click on it, it will show the items.phtml file and highlight the News menu item as active (impressive I know smile ).  Now this is where I run into trouble trying to expand it.  I would like to create a Block that is called using the Grid Widget.

/LC/News/Block/Adminhtml/News.php

<?php

class LC_Adminhtml_Block_News_News extends Mage_Adminhtml_Block_Widget_Grid_Container
{
    
public function __construct()
    
{
        $this
->_controller 'items';
        
$this->_headerText Mage::helper('news')->__('News Manager');
        
$this->_addButtonLabel Mage::helper('news')->__('Add News Item');
        
parent::__construct();
    
}
}

And this is the error that I end up with:

Fatal errorCannot redeclare class LC_Adminhtml_Block_News_News in C:\xampp\htdocs\magento\app\code\local\LC\News\Block\Adminhtml\News.php on line 14

Any thoughts anyone?

-Adam

 
Magento Community Magento Community
Magento Community
Magento Community
 
Moshe
Magento Team
 
Avatar
Total Posts:  1771
Joined:  2007-08-07
Los Angeles
 

@alistek: if it’s /LC/News/Block/Adminhtml/News.php probably should be LC_News_Block_Adminhtml_News

 Signature 

- I would love to change the world, but they won’t give me the source code -

 
Magento Community Magento Community
Magento Community
Magento Community
 
nicolas46
Sr. Member
 
Total Posts:  210
Joined:  2008-04-09
Toulouse
 

Thanks a lot for this tutorial !!
Bravo.

Nico

[HS]
@Moshe : It would be great for us if you can take a look at this post : delete test order-customer
Please, thanks a lot
[/HS]

 
Magento Community Magento Community
Magento Community
Magento Community
 
JWoods
Jr. Member
 
Total Posts:  9
Joined:  2008-04-23
Boston MA
 

This is great work people, thanks for the help. I had gotten a couple hours into doing this without guidance before I found this post.

I’m interested though in knowing how you know all of this. I just can’t seem to follow the directory structure, naming conventions, etc well enough yet to really set out on the several modules we need to build from scratch - a blog being one of them.

I keep thinking there’s some great resource out there I don’t know about but i’ve been pretty much everywhere on this site.

Thanks and keep up the good work.
Joe

 
Magento Community Magento Community
Magento Community
Magento Community
 
alistek
Sr. Member
 
Total Posts:  293
Joined:  2008-04-02
Normal, IL
 

@Moshe

Didn’t work, is there something I am missing in the config.xml to declare what block I am using for adminhtml?

<block>
    <
adminhtml>
        <
news>
            <class>
LC_News_Adminhtml_Block_News</class>
        </
news>
    </
adminhtml>
</
block>

Or could it be having trouble finding the grid widget since I am doing this outside of the Mage namespace?

@JWoods

There isn’t really any resource out there, the best so far has been the PHP|Architect book but even that is fairly incomplete but at least it is a start.  Magento has a high learning curve for learning the architecture and it can be very challenging.  Really I just kept at it until I figured out enough and was a lot of trial and error.  I am trying to figure out as much as I can and get it up here for others.

-Adam

 
Magento Community Magento Community
Magento Community
Magento Community
 
Moshe
Magento Team
 
Avatar
Total Posts:  1771
Joined:  2007-08-07
Los Angeles
 

@alistek: this block will be declared by general <blocks> declaration for this module you’ve made before:

<config>
...
    <global>
...
        <
blocks>
            <
news>
                <class>
LC_News_Block</class>
            </
news>
        </
blocks>
    </global>
</
config>
And called as type="news/adminhtml_news"

 Signature 

- I would love to change the world, but they won’t give me the source code -

 
Magento Community Magento Community
Magento Community
Magento Community
 
alistek
Sr. Member
 
Total Posts:  293
Joined:  2008-04-02
Normal, IL
 

@Moshe

In my NewsController.php

public function indexAction()
    
{
        $this
->loadLayout();
        
$this->_setActiveMenu('news/items');
        
$this->_addContent($this->getLayout()->createBlock('news/adminhtml_news'));
        
$this->renderLayout();
    
}

And that is calling the right block, obviously if I take that out then it just pulls in the news.phtml file in the design section of adminhtml.

No matter what change I make to the the class definition it does not want to work and just gives the “Cannot redeclare class error”.  I am about to duplicate this module and change all LC references to Mage (in a corresponding Mage directory under local) to see if maybe the namespace is causing a problem.

/LC/News/Block/Adminhtml/News.php

<?php

class LC_News_Adminhtml_Block_News extends Mage_Adminhtml_Block_Widget_Grid_Container
{

    
public function __construct()
    
{
        $this
->_controller 'items';
        
$this->_headerText Mage::helper('news')->__('News Manager');
        
$this->_addButtonLabel Mage::helper('news')->__('Add News Item');
        
parent::__construct();
    
}

}

Any thoughts?

EDIT: Nope not a namespace issue, I changed everything to Mage and it still gave the same error.

-Adam

 
Magento Community Magento Community
Magento Community
Magento Community
 
alistek
Sr. Member
 
Total Posts:  293
Joined:  2008-04-02
Normal, IL
 

Ok, after banging my head against the wall forever it was a simple change.  It doesn’t make sense why this works and it could be a bug, but it will let me redeclare the class when I change around the class declaration.

This:

class LC_News_Adminhtml_Block_News extends Mage_Adminhtml_Block_Widget_Grid_Container

To this:

class LC_News_Block_Adminhtml_News extends Mage_Adminhtml_Block_Widget_Grid_Container

-Adam

P.S. I am going as fast as I can, but many more bugs to work out.  I will keep this updated as I go.

 
Magento Community Magento Community
Magento Community
Magento Community
 
Moshe
Magento Team
 
Avatar
Total Posts:  1771
Joined:  2007-08-07
Los Angeles
 

@alistek: erm.. see comment #17 smile

 Signature 

- I would love to change the world, but they won’t give me the source code -

 
Magento Community Magento Community
Magento Community
Magento Community
 
alistek
Sr. Member
 
Total Posts:  293
Joined:  2008-04-02
Normal, IL
 

Haha, oh man I suck smile

-Adam

 
Magento Community Magento Community
Magento Community
Magento Community
 
Moshe
Magento Team
 
Avatar
Total Posts:  1771
Joined:  2007-08-07
Los Angeles
 

@alistek: i disagree, you’re doing great job with this module.

 Signature 

- I would love to change the world, but they won’t give me the source code -

 
Magento Community Magento Community
Magento Community
Magento Community
 
alistek
Sr. Member
 
Total Posts:  293
Joined:  2008-04-02
Normal, IL
 

Thank you, I appreciate the words of encouragement.  I know how much time it takes to answer all these questions, especially with the work load you guys have.  Any little bit is appreciated and I am glad that I can help take a bit of work off of your guy’s shoulders and give back.

-Adam

 
Magento Community Magento Community
Magento Community
Magento Community
 
alistek
Sr. Member
 
Total Posts:  293
Joined:  2008-04-02
Normal, IL
 

Ok I have another question here.

I am getting this error:

Fatal errorCall to a member function setSaveParametersInSession() on a non-object in C:\xampp\htdocs\magento\app\code\core\Mage\Adminhtml\Block\Widget\Grid\Container.php on line 53

Most likely related to this code in /app/code/core/Mage/Adminhtml/Block/Widget/Grid/Container.php in _prepareLayout:

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

I would assume this is because I am missing a required file and it is related to the controller.  Would I need a NewsControllerGrid.php or something like that?

Here is my News.php Block:

<?php

class LC_News_Block_Adminhtml_News extends Mage_Adminhtml_Block_Widget_Grid_Container
{

    
public function __construct()
    
{
        $this
->_controller 'news';
        
$this->_headerText Mage::helper('news')->__('News Manager');
        
$this->_addButtonLabel Mage::helper('news')->__('Add News');
        
parent::__construct();
    
}

}

-Adam

 
Magento Community Magento Community
Magento Community
Magento Community
 
Moshe
Magento Team
 
Avatar
Total Posts:  1771
Joined:  2007-08-07
Los Angeles
 

what yields:

echo $this->_blockGroup.'/' $this->_controller '_grid';

The code relies on this block class.

 Signature 

- I would love to change the world, but they won’t give me the source code -

 
Magento Community Magento Community
Magento Community
Magento Community
 
alistek
Sr. Member
 
Total Posts:  293
Joined:  2008-04-02
Normal, IL
 

I just noticed that and I did a var_dump on $this.  It looks like if nothing is entered for _controller or _blockGroup it defaults to adminhtml/empty_grid.  If I set _controller = ‘ news’ then I will get adminhtml/news_grid and this doesn’t exist with _grid. 

Would I have to change block group and controller?

$this->_controller 'adminhtml_news';
$this->_blockGroup 'news';

Then it generates news/adminhtml_news_grid.

Right now this class doesn’t exist so I will have to create it, but would that be correct you think?

-Adam

 
Magento Community Magento Community
Magento Community
Magento Community
Magento Community
Magento Community
    Back to top
Page 2 of 14
 
Sales: Call 877.832.5289 (North America) 310.295.4144 (International)
© Copyright 2008 Varien. Magento, eCommerce software, is a trademark of Irubin Consulting Inc. DBA Varien
Privacy Policy|Terms of Service
Magento Community Count
52318 users|433 users currently online|105654 forum posts