Advertisement
Guest User

Untitled

a guest
Nov 20th, 2013
289
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.26 KB | None | 0 0
  1. You can create a new table when you install the module like below:
  2.  
  3. If you look in any normal module in phpfox that uses the import product method they have a install file that goes on:
  4.  
  5. "folder where phpfox is installed" \include\xml\YourModuleName.xml
  6.  
  7. This is the file that installs the module,without it,the module won't show in the import products. You can create,update,delete tables using this file.
  8.  
  9. Below is an example:
  10.  
  11. [quote]
  12. <product>
  13. <data>
  14. <product_id>YourModuleName</product_id>
  15. <is_core>0</is_core>
  16. <title>My Module</title>
  17. <description>Learning how to modify the database</description>
  18. <version>2.1</version>
  19. <latest_version></latest_version>
  20. <last_check>1323010924</last_check>
  21. <is_active>1</is_active>
  22. <url></url>
  23. <url_version_check></url_version_check>
  24. </data>
  25. <installs>
  26. <install>
  27. <version>0.1</version>
  28. <install_code><![CDATA[$this->database()->query("CREATE TABLE IF NOT EXISTS `".Phpfox::getT('yourtablename')."` (
  29. `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  30. `user_id` int(11) unsigned NOT NULL,
  31. `destination` varchar(255) NOT NULL,
  32. `time_stamp` int(11) NOT NULL DEFAULT '0',
  33. PRIMARY KEY (`id`)
  34. ) AUTO_INCREMENT=1 ");]]></install_code>
  35. <uninstall_code><![CDATA[$this->database()->query("DROP TABLE `" . Phpfox::getT('yourtablename') . "`");]]></uninstall_code>
  36. </install>
  37. </installs>
  38. <modules>
  39. <module_id>yourmodulename</module_id>
  40. </modules>
  41. </product>
  42. [/quote]
  43.  
  44. The code above will create a table called 'yourtablename' with columns id,user_id,destination,time_stamp.
  45.  
  46. You can also see the uninstall code, which will delete this table when you uninstall the module.
  47.  
  48. Queries:
  49.  
  50. Create a table:
  51. create a table called 'mytable' with columns id,user_id,destination,time_stamp.
  52.  
  53. $this->database()->query("CREATE TABLE IF NOT EXISTS `".Phpfox::getT('mytable')."` (
  54. `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  55. `user_id` int(11) unsigned NOT NULL,
  56. `destination` varchar(255) NOT NULL,
  57. `time_stamp` int(11) NOT NULL DEFAULT '0',
  58. PRIMARY KEY (`id`)
  59. ) AUTO_INCREMENT=1 ");
  60.  
  61.  
  62. Delete a table:
  63. $this->database()->query("DROP TABLE `" . Phpfox::getT('yourtablename') . "`");
  64.  
  65. Modify an existing table.
  66. In this example we modify the table "user" and add a new column called "is_verified"
  67. $this->database()->query("ALTER TABLE `".Phpfox::getT('user')."` ADD `is_verified` TINYINT( 1 ) UNSIGNED NOT NULL DEFAULT '0'");
  68.  
  69. With this code we delete the column that we just created in the step above.
  70. $this->database()->query("ALTER TABLE `" . Phpfox::getT('user') . "` DROP COLUMN `is_verified`");
  71.  
  72. Delete rows in a table that matches specific values in a field.
  73. $this->database()->query("DELETE FROM `" . Phpfox::getT('tablename') . "` WHERE type_id='user_verify'");
  74.  
  75. Update rows in a table that matches specific values in a field.
  76. $this->database()->query("UPDATE `" . Phpfox::getT('tablename') . "` SET `value_actual` = '0' WHERE `setting_id` = '344'");
  77.  
  78. You can just paste any code above in the install/uninstall text field when you create a module via AdminCP.
  79.  
  80. Please use the code above in a test enviroment until you feel your module is ready to be used on a live site,so you won't mess up with your database while testing.
  81.  
  82. Regards,
  83. Robert
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement