Fork me on GitHub

Help - creating 2 tables in mod init via .760 method  Bottom

  • I want to start using the new .760 methodology for creating modules and having a hard time with the init phase and creating two tables. Can someone post a generic example of a mod_init() function and a mod_pntables() funtion that would successfully create two tables assuming the following.

    Code

    $table1_flds = "
    $FileShare_filescolumn[tid] I AUTOINCREMENT PRIMARY,
    $FileShare_filescolumn[cid] I (3) NOTNULL DEFAULT '',
    $FileShare_filescolumn[title] VARCHAR (100) NOTNULL DEFAULT '',
    $FileShare_filescolumn[description] TEXT NOTNULL DEFAULT '',
    $FileShare_filescolumn[filename]     VARCHAR (32) NOTNULL DEFAULT ''
    "
    ;

    $table2_flds = "
    $FileShare_categoriescolumn[tid] I AUTOINCREMENT PRIMARY,
    $FileShare_categoriescolumn[title] VARCHAR (100) NOTNULL DEFAULT '',
    $FileShare_categoriescolumn[description] TEXT NOTNULL DEFAULT '',
    $FileShare_categoriescolumn[filename]    VARCHAR (32) NOTNULL DEFAULT ''
    "
    ;


    Thanks in advance!
  • Alar,

    As a reference, look at the includes/classes/adodb/datadict/datadict-mysql.inc.php

    In it you'll see the adodb calls for generating the different types of fields in mysql.

    I see that you are using the I (adodb mysql returns Integer) but are not using C (VARCHAR for adodb mysql datadict).

    You'll have to make all your field descriptions match the datadict conversions -

    I for integer
    C for varchar, etc.

    UHEWeb
  • The part of the module init script that's required for each table is

    Code

    // 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!)

    Code

    // 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.
  • Would it be appropriate to use the adodb xml schema from that link you passed I saw that the adodb-xmlschema.inc.php is part of the includes in the adodb directory.
  • for instance if we saved the table entries in the schema file as xml when the the $schema->ParseSchema( $schemaFile ); is called can we replace each table name= with name = prefix + name?
  • Hmm... I somehow came unsubscribed from this topic. I just wanted to say thanks for the tips guys, I appreciate the assist!

    :)
  • loop idea is nice, thanks!

This list is based on users active over the last 60 minutes.