User Login    
 + Register
  • Main navigation
Login
Username:

Password:


Lost Password?

Register now!
Documentation
Who's Online
89 user(s) are online (8 user(s) are browsing XoopsWiki)

Members: 2
Guests: 87

meezs, jdseymour, more...
[Main Page]

Dev:database tables

From XOOPS Project

Main Page | Recent changes | Edit this page | Page history | Switch to MediaWiki mode

Printable version | Disclaimers | Privacy policy
Category: Development

Contents

Creating tables

XOOPS creates database tables for your module when the module is first installed. It does this by executing instructions found in your module .sql file. The location of your module’s sql file is specified in your module’s xoops_version.php file :

   $modversion['sqlfile']['mysql'] = "sql/mysql.sql";

The sql file should contain a list of CREATE TABLE commands, needed for your module. An example of the format is :

CREATE TABLE 'modulename_tablename' (

   'id' int(12) NOT NULL auto_increment,
   'varcharfield' varchar(120),
   PRIMARY KEY ('id', 'varcharfield'),

);

A simple way of creating your sql file is to create a 'dump' file of your development database, with only the tables necessary for your module included. In phpMyAdmin this is done by clicking the 'export' tab, then select the tables to export (don’t include 'drop tables' or the data). You will still need to delete the database prefix from your sql file by hand though.

MySQL reference manual for CREATE TABLE syntax: http://dev.mysql.com/doc/mysql/en/create-table.html

Adding records to the tables at install time

If you want some records inserted into your newly created tables you can use the onInstall function. This function is called directly after the tables have been created from the sql file. So any INSERT sql commands can be written in that function. Again, the location of the file containing your onInstall function is given in the xoops_version.php file :

   $modversion['onInstall'] = "path/to/file/with/onInstall/function";

The function takes a XoopsModule object as parameter and should be in the form of :

function xoops_module_install_[dirname](&$module) {

   ...

}

Finish the function with returning either true or false for success or failure, respectively.

See also: xoops install uninstall

MySQL reference manual for INSERT syntax: http://dev.mysql.com/doc/mysql/en/insert.html

Altering tables

As of XOOPS version 2.0.10 there is also an onUpdate function available. This can be used for altering the database tables for your module when the structure has been changed in a new version. Your module’s xoops_version.php is used to specify the location of the file that contains the onUpdate function.

   $modversion['onUpdate'] = "path/to/file/with/onUpdate/function";

The function takes two parameters and should be in the form of :

function xoops_module_update_[dirname](&$module, $old_version) {

   ...

}

The $module parameter is a reference to the (updated) XoopsModule object, which you can use or not use in the function - it is up to you. You can give feedback to the update module listing by calling

   $module->setErrors($feedback_message_as_string);

inside the function.

The second parameter is the module's old version. Note that this is 100 * version as specified in xoops_version.php so if your module's old version is 2.1, $old_version will hold the value 210.

Finish the function with returning either true or false for success or failure, respectively.

See also: xoops install uninstall

Deleting tables

Tables for your module should be deleted when your module is uninstalled. To ensure that all tables are deleted they must all be listed in the xoops_version.php file.

   $modversion['tables'][0] = "modulename_category";
   $modversion['tables'][1] = "modulename_article";

There is also an optional onUninstall function available which is run after the module is uninstalled. Once again, give the location of the file containing your uninstall function in the xoops_version.php file :

   $modversion['onUninstall'] = "path/to/file/with/onUninstall/function";

The function takes a XoopsModule object as parameter and should be in the form of :

function xoops_module_uninstall_[dirname](&$module) {

   ...

}

Finish the function with returning either true or false for success or failure, respectively.

See also: xoops install uninstall

back to Main Page

Retrieved from "http://www.xoops.org/modules/mediawiki/index.php/Dev:database_tables"

This page has been accessed 443 times. This page was last modified 01:50, 16 December 2007. Content is available under XOOPS Project.


Local Support Sites
Powered by
XOOPS Code hosted on SourceForge

Powered by PHP



Powered by MySQL

Powered by Smarty

OSI certified

All content on this site is subject to the Creative Commons License
Developers for Hire