Note:

If you want to create a new page for developers, you should create it on the Moodle Developer Resource site.

Using XMLDB

From MoodleDocs
Revision as of 12:54, 10 January 2008 by Pat B. (talk | contribs)

(This page is in progress of writing and editing, intended for third-party module developers to help them switch to XMLDB.)

How XMLDB can help you

Everywhere in Moodle now use XMLDB. XMLDB is database format created to prevent developer from creating custom database schemas for different database system. If you don't know what XMLDB is, You can learn more about it here XMLDB_introduction

In every module folder, there is db/ folder where database structure stored. The folder usually contains mysql.php, mysql.sql, etc for different database.

It's mean that you have to create different files for each database systems. If there are 5 database, you have to create 10 files (2 each). XMLDB is intended to solve this. Switching to XMLDB is very easy, save your time and effort by creating just 1 file.

Getting Started

XMLDB file in module is stored in db/ folder, named install.xml. You can use a built-in XMLDB editor to create or edit any install.xml files. If you're using MySQL as your default database schemes. XMLDB editor's powerful tools allowing retrofit MySQL table to XMLDB format.

XMLDB editor is located in administration block, go to Miscellaneous > XMLDB editor. You may unable to click on Create link and also can't edit your module. Since you must make your module folder has write permission first (And if you haven't craeted db/ folder in your module folder yet, create it now). In Linux operating system, chmod your /db folder to 777 solves this. Now you're ready to create your module's XMLDB files.

Note that XMLDB editor is not a tool for creating your module. It is a tool to manage XMLDB database file (think about phpmyadmin manages SQL database). You can create tables/fields/keys/indexes or export your exist MySQL tables to XMLDB.

XMLDB editor is really easy to work with. By playing with it, you will soon understand XMLDB structure and format.

More information about XMLDB editor, XMLDB Structure : XMLDB defining an XML structure.

Create install.xml

If you never create install.xml before (your db/ doesn't have install.xml), click Create first. install.xml will store your database info for this module.

Start edit

To start edit install.xml, click Load and then Edit. You need to do this every time before editing any XMLDB file. If this is your first time, there is default table named like your module stored.

Retrofit MySQL table to XMLDB

XMLDB editor can retrofit MySQL table to XMLDB. This is useful if your module database already using MySQL. First, your module has to be installed. Then follow these steps:

  1. Go to XMLDB editor, edit your module.
  2. Click New Table from MySQL
  3. In Create Table box, choose your table from MySQL database. In After Table choose where would you like to add this table to (It doesn't matter but helpful to sort it). Click Create.
  4. Do it again untill every table added, you are done !

Note : Table's name must contain only character, number, underscore and length must not exceed 28 characters.