The part of the module init script that's required for each table is
// It's good practice to name the table and column definitions you
// are getting - $table and $column don't cut it in more complex
// modules
$Exampletable = &$pntable['example'];
$Examplecolumn = &$pntable['example_column'];
// Define the fields in the form:
// $fieldname $type $colsize $otheroptions
$flds = "
$Examplecolumn[tid] I AUTOINCREMENT PRIMARY,
$Examplecolumn[itemname] C (32) NOTNULL DEFAULT '',
$Examplecolumn[number] I4 NOTNULL DEFAULT 0
";
// Creating the table
$sqlarray = $dict->CreateTableSQL($Exampletable, $flds, $taboptarray);
// Check for an error with the database code, and if so set an
// appropriate error message and return
if ($dict->ExecuteSQLArray($sqlarray) != 2) {
pnSessionSetVar('errormsg', _EXAMPLECREATETABLEFAILED);
return false;
}
The first section should already be familiar - this defines the table from the pntables array.
The second section defines the schema of the table using ADODB's
data dictionary
The third part gets our
data dictionary object to turn our field and table information into some
SQL for our specific database engine. The fourth part executes this
SQL. Note: The method names mention arrays as although the create table method won't return multiple
SQL statements other methods (
e.g. alter table) might.
So putting this together (and adding a loop to make it neater!)
// It's good practice to name the table and column definitions you
// are getting - $table and $column don't cut it in more complex
// modules
$Exampletable = &$pntable['example'];
$Examplecolumn = &$pntable['example_column'];
$Example2table = &$pntable['example2'];
$Example2column = &$pntable['example2_column'];
// Define the fields in the form:
// $fieldname $type $colsize $otheroptions
$schema[$Exampletable] = "
$Examplecolumn[tid] I AUTOINCREMENT PRIMARY,
$Examplecolumn[itemname] C (32) NOTNULL DEFAULT '',
$Examplecolumn[number] I4 NOTNULL DEFAULT 0
";
// Define the fields in the form:
// $fieldname $type $colsize $otheroptions
$schema[$Example2table] = "
$Example2column[tid] I AUTOINCREMENT PRIMARY,
$Example2column[itemname] C (32) NOTNULL DEFAULT '',
$Example2column[number] I4 NOTNULL DEFAULT 0
";
foreach ($schema as $table => $flds) {
// Creating the table
$sqlarray = $dict->CreateTableSQL($table, $flds, $taboptarray);
// Check for an error with the database code, and if so set an
// appropriate error message and return
if ($dict->ExecuteSQLArray($sqlarray) != 2) {
pnSessionSetVar('errormsg', _EXAMPLECREATETABLEFAILED);
return false;
}
}
Note: Like many of my posts including code this has been typed directly into the forum and thus may (will) contain errors but you should be able to extract the general idea.
-Mark
--
Visit
My homepage and
Zikula themes.