Tutorials: Tutorial: How to update tables to follow XOOPS' new naming scheme?
Posted by: MambaOn 2013/3/5 4:20:00 6479 readsAs you might already know, there is an effort to standardize our module development - from using the same module Admin GUI structure, to using the same icons across all modules, from using the same pagination structure for each table, to naming the tables and fields in a consistent way (see this thread). This tutorial will show you how to modify your module so it can rename the tables on the user site, when the user updates the module. This will follow the scheme suggested by alain01 The new table naming scheme is: mod_AAA_BBBB where AAA is the name of the module, and BBB is the name of the table. For example, when we have in the News module a table called "topics", in the new updated version of News, it will become: mod_news_topics Here are few steps to follow, as used recently in the Pedigree module called "animal": 1) The new version should have the tables defined properly in the SQL file, so new installation have the right tables installed right away 2) In the existing installation the users normally copy files over, and then run "update" in the Admin. Therefore we'll need to add a file with the updates. We'll call it "update_function.php" and will place it in /include folder 3) In order for XOOPS to call this file, we'll add in xoops_version.php file following:
$modversion['onUpdate'] = 'include/update_function.php';
function tableExists($tablename)
{
global $xoopsDB;
$result=$xoopsDB->queryF("SHOW TABLES LIKE '$tablename'");
return($xoopsDB->getRowsNum($result) > 0);
}
function xoops_module_update_animal()
{
global $xoopsDB;
if (tableExists($xoopsDB->prefix('eigenaar'))) {
$sql = sprintf(
'ALTER TABLE ' . $xoopsDB->prefix('eigenaar') . ' RENAME ' . $xoopsDB->prefix('mod_pedigree_owner')
);
$result = $xoopsDB->queryF($sql);
if (!$result) {
echo '
' . _AM_PED_UPGRADEFAILED . ' ' . _AM_PED_UPGRADEFAILED2;
$errors++;
}
}
return TRUE;
}