1
makakazo
How to set foreign keys when installing a module
  • 2007/2/15 11:56

  • makakazo

  • Just popping in

  • Posts: 10

  • Since: 2007/1/11


Hi, I'm coding my first XOOPS module from scratch, and I'd like to know how to set foreign keys to my DBs tables at the time of installation. I can insert data directly in the sql file where I create my tables, but haven't found a way to assign foreign keys.

I guess I'll have to use 'oninstall' in xoops_version but but I can't find out how to use that 'oninstall' functionaluty :S

2
McDonald
Re: How to set foreign keys when installing a module
  • 2007/2/15 12:15

  • McDonald

  • Home away from home

  • Posts: 1072

  • Since: 2005/8/15


You mean this?

// Additionnal script executed during install update
$modversion['onInstall'] = 'include/install.php';

//$modversion['onUpdate'] = 'include/update.php';
$modversion['onUninstall'] = 'include/uninstall.php';


This part comes from the file xoops_version.php of the module Pages.

3
makakazo
Re: How to set foreign keys when installing a module
  • 2007/2/15 14:17

  • makakazo

  • Just popping in

  • Posts: 10

  • Since: 2007/1/11


I think I can get the oninstall to work.

The point is that I can add constraints fo the foreign keys in the sql file so they are executed when the tables are created, but they only work if I add the prefix that my XOOPS site uses for tables, so the module wouldn't install on any other site.

I think the only way is with the fuction called in the file specified by "$modversion['onInstall'] = 'include/install.php';", connecting to the DB to run an alter table query.

Am I wrong?

4
iHackCode
Re: How to set foreign keys when installing a module

Here are some links to the wiki. (but i noticed that the first link 'XoopsInstallUninstall' that a different variable is passed . compared to the 'DatabaseTables' link
http://dev.xoops.org/modules/phpwiki/index.php/XoopsInstallUninstall (info on the on functions)
http://dev.xoops.org/modules/phpwiki/index.php/DatabaseTables (a little bit of more info)
http://dev.xoops.org/modules/phpwiki/index.php/ModVersion (mostly info on the xoops_version.php File. but also has more info about onInstall and stuff)

here is an example from a file that is used by a module with the onInstall function.
[color=006600/** xmspotlight onInstall Function . Used in xoops_version.php at module install time
 *
**/ 
[/color]
function 
xoops_module_install_xmspotlight(&$module){    
    global 
$xoopsDB;

    [
color=006600]//add field in stories table for link to img for article or spotlight.[/color]
    
if (!FieldExists('imgspotlight',$xoopsDB->prefix('stories'))) {
 [
color=006600// Adds Field to the table  [i]prefix[/i]_stories[/color]
      
$sql sprintf("ALTER TABLE ".$xoopsDB->prefix('stories'). " ADD imgspotlight varchar(255) NOT NULL default ' ' ");    
    
$result =    $xoopsDB->query($sql);
    }
    
//add field in stories table for text for img credits
    
if (!FieldExists('imgauthor',$xoopsDB->prefix('stories'))) {
      
$sql sprintf("ALTER TABLE ".$xoopsDB->prefix('stories')." ADD imgauthor varchar(255) NOT NULL default ' ' ");
      
$result $xoopsDB->query($sql);
    }
return 
true;
}

/**
 * Taken from News Module.
 * Description: Checks if field exists in a table 
**/
function FieldExists($fieldname,$table)
{
    global 
$xoopsDB;
    
$result=$xoopsDB->queryF("SHOW COLUMNS FROM    $table LIKE '$fieldname'");
    return(
$xoopsDB->getRowsNum($result) > 0);
}


hope that helps some.
CBB / LatestNews / Publisher / XM-Spotlight

(ノ◕ヮ◕)ノ*:・゚✧

5
makakazo
Re: How to set foreign keys when installing a module
  • 2007/2/15 16:51

  • makakazo

  • Just popping in

  • Posts: 10

  • Since: 2007/1/11


Yes Bandit-X, that sure helps, it was what I was looking for

EDIT: OK, it's working fine now.

Anyway, I think that the parsing system used when executing the sql file specified in

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

should allow to introduce constraints as well. You can have "create table" and "insert into" sentences and it adds the DB prefix to all table names right, but constraints are not supported in this way, so you have to use alter table afterwards using "oninstall"

Login

Who's Online

448 user(s) are online (326 user(s) are browsing Support Forums)


Members: 0


Guests: 448


more...

Donat-O-Meter

Stats
Goal: $100.00
Due Date: Nov 30
Gross Amount: $0.00
Net Balance: $0.00
Left to go: $100.00
Make donations with PayPal!

Latest GitHub Commits