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

Magento

eCommerce Software for Online Growth

Magento Forum

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

PLEASE SEE THIS WIKI ARTICLE:

UPDATE!!  It is finally done, Part 2 is in the WIKI for creating the backend!!  Have fun everyone!!

PLEASE NOTE THAT THE WIKI IS DIFFERENT THEN THIS THREAD, I MADE CHANGES TO THE TUTORIAL.  SEE THE WIKI FOR THE MOST UP TO DATE!!!

http://www.magentocommerce.com/wiki/custom_module_with_custom_database_table

-Adam

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

Please see:

http://www.magentocommerce.com/wiki/custom_module_with_custom_database_table

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

Please see:

http://www.magentocommerce.com/wiki/custom_module_with_custom_database_table

 
Magento Community Magento Community
Magento Community
Magento Community
 
PJ
Jr. Member
 
Total Posts:  26
Joined:  2007-08-31
 

Bravo!  Keep up the good work.  I learned a great deal with your tutorial. Thanks again.

 
Magento Community Magento Community
Magento Community
Magento Community
 
maZe
Jr. Member
 
Total Posts:  13
Joined:  2007-12-27
Würzburg
 

Good Work, Great Tutorial.
Looking foward to seeing the admin tutorial.
I am experiencing Problems with creating an admin module.

Matthias

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

Thanks!  I haven’t even looked at the admin part yet so I have no idea how bad that will be to figure out, hopefully not awful but I am going to try and figure out as much as I can in the next few days so I can get something posted here.

-Adam

 
Magento Community Magento Community
Magento Community
Magento Community
 
Anders Rasmussen
Sr. Member
 
Avatar
Total Posts:  168
Joined:  2007-10-16
Denmark
 

Great work! One thing though: I see you have a setup script to create the table needed for the model. When will this script be run? I mean, how do you install the module?

 Signature 

Anders Rasmussen
Crius (in Danish)

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

The script is run if the news table does not exist in the database, it will check upon the first instance of the module being accessed.  I didn’t put any sample data in there but I should probably modify it for that or else nothing will be outputted.

-Adam

 
Magento Community Magento Community
Magento Community
Magento Community
 
danieln
Sr. Member
 
Avatar
Total Posts:  100
Joined:  2007-11-08
Mainz, Germany
 

Hello alistek,

thats great! I’ve done some modules too and I’m really fed up with this painfull and time consuming process. I took your files and included them into a small Module Creator Script. This will make it even more easier to write custom modules for everyone.

Just dump the content of the zip file to any document root, call the folder and enter your Namespace and Module name. All required files will be generated. All what’s left to do is copy the files and folders to your Magento installation (and of course implement functionallity).

Hope this will help someone wink

Best regards,
daniel

Update 23/06/2008:
Added Adminhtml Support to ModuleCreator.

File Attachments
moduleCreator0.0.3.zip  (File Size: 10KB - Downloads: 67)
moduleCreator0.0.2.zip  (File Size: 10KB - Downloads: 36)
moduleCreator0.0.4.zip  (File Size: 19KB - Downloads: 82)
 Signature 

netz98.de - new media gmbh - First Magento Gold Partner Germany.

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

See this is what community is all about wink Thanks for the great work danieln!  I am working on the adminhtml part of it now and making good progress, faster than I would have thought so hopefully I will have something in the next day or so to have up here.

-Adam

 
Magento Community Magento Community
Magento Community
Magento Community
 
maZe
Jr. Member
 
Total Posts:  13
Joined:  2007-12-27
Würzburg
 

@danieln: Thank you very much… that’s a very useful Tool.
I tried out and it did what it had to do wink. But I found a little bug:
The Model Classname was not generated correctly, still have <Namespace_Module> at the beginning.
Everything else looks pretty good.
Do you know how to create an Admin module ?

Thanks again
maZe

 
Magento Community Magento Community
Magento Community
Magento Community
 
danieln
Sr. Member
 
Avatar
Total Posts:  100
Joined:  2007-11-08
Mainz, Germany
 

@maZe: You’re right, I fixed it. Thank you!

 Signature 

netz98.de - new media gmbh - First Magento Gold Partner Germany.

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

I am working on the admin module right now, I have it where it will create a menu item and display stuff in a phtml file.  I am trying to get it to pull data through the grid view widget with an edit screen since that is the most common request usually.  As soon as I have it working and some of these bugs ironed out I will update here.

-Adam

 
Magento Community Magento Community
Magento Community
Magento Community
 
maZe
Jr. Member
 
Total Posts:  13
Joined:  2007-12-27
Würzburg
 

@alistek: that would be very great, right now I am stuck with Adminhtml Blocks and furthermore how to sperate the files correctly.
Looking forward to seeing your solution.

maZe

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

Yeah, I am doing my best but I keep running into an error with trying to use the grid widgets on the page, telling me it can’t extend the class and no matter what combination I try something isn’t right.  It will take me a bit longer than I had hoped, does anyone else have any experience extending that?

-Adam

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

Alright guys I have hit a bit of a wall.  It is fairly easy to get the backend working to display a menu item and a custom page, etc.  But I am trying to use the grid class so that I can autocreate and load the data with edit forms like other Magento modules do.  Here is the code so far:

Note: I am not generalizing it until I get this working correctly.

Expanded Directory Structure:

/app/code/local/
                            
LC/
                                  
News/
                                             
Block/
                                                        
Adminhtml/
                                             
controllers/
                                                                 
Adminhtml/
                                             
etc/
                                             
Model/
                                                         
Mysql4/
                                                                     
News/
                                            
sql/
                                                 
news_setup/
/
app/design/frontend/default/default/template/
                                                                                  
news/
/
app/design/adminhtml/default/default/template/
                                                                                       
news/

Now I needed to add 3 main files.

/app/code/local/LC/News/controllers/Adminhtml/ItemsController.php

<?php

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

?>

/app/design/adminhtml/default/default/layout/news.xml

<?xml version="1.0"?>
<layout version="0.1.0">
    <
news_adminhtml_items_index>
        <
reference name="content">
            <
block type="adminhtml/template" name="items" template="news/items.phtml" />
        </
reference>
    </
news_adminhtml_items_index>
</
layout>

/app/design/adminhtml/default/default/template/news/items.phtml
Note: This doesn’t do anything really other then display text just to show it’s working.

<h3>News Module</h3>

Updated:
/app/code/local/LC/News/etc/config.xml

<?xml version="1.0"?>
<config>
    <
modules>
        <
LC_News>
            <
version>0.1.0</version>
        </
LC_News>
    </
modules>
    <
frontend>
        <
routers>
            <
news>
                <use>
standard</use>
                <
args>
                    <
module>LC_News</module>
                    <
frontName>news</frontName>
                </
args>
            </
news>
        </
routers>
        <
layout>
            <
updates>
                <
news>
                    <
file>news.xml</file>
                </
news>
            </
updates>
        </
layout>
    </
frontend>
    <
admin>
        <
routers>
            <
news>
                <use>
admin</use>
                <
args>
                    <
module>LC_News</module>
                    <
frontName>news</frontName>
                </
args>
            </
news>
        </
routers>
    </
admin>
    <
adminhtml>
        <
menu>
            <
news module="news">
                <
title>News</title>
                <
sort_order>71</sort_order>                
                <
children>
                    <
items module="news">
                        <
title>Manage News</title>
                        <
sort_order>0</sort_order>
                        <
action>news/adminhtml_items</action>
                    </
items>
                </
children>
            </
news>
        </
menu>
        <
acl>
            <
resources>
                <
admin>
                    <
children>
                        <
news>
                            <
children>
                                <
title>Manage News</title>
                            </
children>
                        </
news>
                    </
children>
                </
admin>
            </
resources>
        </
acl>
        <
layout>
            <
updates>
                <
news>
                    <
file>news.xml</file>
                </
news>
            </
updates>
        </
layout>
    </
adminhtml>
    <global>
        <
helpers>
            <
news>
                <class>
LC_News_Helper</class>
            </
news>
        </
helpers>
        <
models>
            <
news>
                <class>
LC_News_Model</class>
                <
resourceModel>news_mysql4</resourceModel>
            </
news>
            <
news_mysql4>
                <class>
LC_News_Model_Mysql4</class>
                <
entities>
                    <
news>
                        <
table>news</table>
                    </
news>
                </
entities>
            </
news_mysql4>
        </
models>
        <
resources>
            <
news_setup>
                <
setup>
                    <
module>LC_News</module>
                </
setup>
                <
connection>
                    <use>
core_setup</use>
                </
connection>
            </
news_setup>
            <
news_write>
                <
connection>
                    <use>
core_write</use>
                </
connection>
            </
news_write>
            <
news_read>
                <
connection>
                    <use>
core_read</use>
                </
connection>
            </
news_read>
        </
resources>
        <
blocks>
            <
news>
                <class>
LC_News_Block</class>
            </
news>
        </
blocks>
    </global>
</
config>

 
Magento Community Magento Community
Magento Community
Magento Community
Magento Community
Magento Community
    Back to top
Page 1 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
53225 users|839 users currently online|107284 forum posts