Creating a simple module/Using the Database

From Joomla! Documentation

< J2.5:Creating a simple module
Revision as of 08:59, 10 May 2013 by Wilsonge (Talk | contribs)

Many modules in Joomla require using a database. It is assumed in this tutorial that you already understand the basics of using the JDatabase class. If you don't please read the documentation on accessing the database using JDatabase before continuing this tutorial

Creating a table on install

To create the xml table on install we are going to add the following lines into mod_helloworld.xml:

         <file driver="mysql" charset="utf8">sql/mysql/install.mysql.utf8.sql</file>
         <file driver="sqlazure" charset="utf8">sql/sqlazure/install.sqlazure.utf8.sql</file>
         <file driver="mysql" charset="utf8">sql/mysql/uninstall.mysql.utf8.sql</file>
         <file driver="sqlazure" charset="utf8">sql/sqlazure/uninstall.sqlazure.utf8.sql</file>
        <schemapath type="mysql">sql/mysql/updates</schemapath> 
	<schemapath type="sqlazure">sql/sqlazure/updates</schemapath> 

There are 3 sections to this code:

  • The install tag adds the database table
  • The uninstall tag removes the database table if the module is uninstalled. Note that not all modules will want to use this feature (and it's not required).
  • The update tag will update the databases if a database needs to be amended when updating the module.

Note that we have both schemas for mysql and microsoft SQL - again you can choose to tailor your module for one or both of these systems.

Making the request in the helper file