The Arcive of Official vBulletin Modifications Site.It is not a VB3 engine, just a parsed copy! |
|
#1
|
|||
|
|||
Installation Tables and Columns Help...
Okay, lets say I have a mod... and in version1, I have the following install code... (the code is an example, I know its not functioning, but thats got nothing to do with my problem)
Code:
$db->query_write("CREATE TABLE IF NOT EXISTS table ( column1 int auto_increment, column2 int, PRIMARY KEY (column1) )"); Then I create a version2, with an extra column... so I made my install code this... Code:
$db->query_write("CREATE TABLE IF NOT EXISTS table ( column1 int auto_increment, column2 int, column3 int, PRIMARY KEY (column1) )"); $db->query_write("ALTER TABLE table ADD COLUMN column3 int)"); Now I am making a version 3... and there is yet another column to add... I tried the following code, but I got an error when it hit the ADD COLUMN column3 code... because column3 already exists. Code:
$db->query_write(" CREATE TABLE IF NOT EXISTS table ( column1 int auto_increment, column2 int, column3 int, column4 int, PRIMARY KEY (column1) )"); $db->query_write("ALTER TABLE table ADD COLUMN column3 int)"); $db->query_write("ALTER TABLE table ADD COLUMN column4 int)"); |
#2
|
||||
|
||||
You could run a query to retrieve the columns in "table" and then control the flow of logic from there.
PHP Code:
|
#3
|
|||
|
|||
Hmm... how would that code be written exactly? What would be the code to construct $columns?
|
#4
|
||||
|
||||
PHP Code:
|
#5
|
|||
|
|||
Huh? I'm confused? What would I need to loop? Wouldn't this be enough?
Code:
$columns = $db->query_read("SHOW COLUMNS FROM table"); if (!in_array('column3', $columns)) { $db->query_write("ALTER TABLE table ADD COLUMN column3 int)"); } if (!in_array('column4', $columns)) { $db->query_write("ALTER TABLE table ADD COLUMN column4 int)"); } |
#6
|
||||
|
||||
If you look at what $columns contains, you'll see it is a two-dimensional array. You need to loop through it after your query to extract the relevant information; in this case - the field names.
PHP Code:
|
#7
|
||||
|
||||
There is a reason why you are allowed to create multiple install codes in products. In the version 1 install code, use the first query. In the version 2 code, create a query using ALTER and add another column. In the version 3 install code, create another query.
On a new installation of the modification, vBulletin will run the install codes in order of version. |
#8
|
|||
|
|||
So this this shoudl work? the table name is video... and the column is timelength...
Code:
$columns = $db->query_read("SHOW COLUMNS FROM video"); $videocolumns = array(); while ($column = $db->fetch_array($columns)) { $videocolumns[] = $column['field']; } if (!in_array('timelength', $videocolumns)) { echo('<li>Altering Table <strong>' . TABLE_PREFIX . 'video</strong> ... '); $db->query_write("ALTER TABLE video ADD timelength int(10) unsigned NOT NULL"); } |
#9
|
||||
|
||||
Why not use the install code functionality now?
|
#10
|
|||
|
|||
Because what if someone is upgrading from Version 1 to Version 3... They wont get the changes in Version 2, because its not in the new install code. If I put the new install code in Version 3; then it gets the redundancy error.
Anyway, the code worked, thanks Kirk! |
Thread Tools | |
Display Modes | |
|
|
X vBulletin 3.8.12 by vBS Debug Information | |
---|---|
|
|
More Information | |
Template Usage:
Phrase Groups Available:
|
Included Files:
Hooks Called:
|