dbtng_example.install 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. <?php
  2. /**
  3. * @file
  4. * Install, update and uninstall functions for the dbtng_example module.
  5. */
  6. /**
  7. * Implements hook_install().
  8. *
  9. * In Drupal 7, there is no need to install schema using this hook, the schema
  10. * is already installed before this hook is called.
  11. *
  12. * We will create a default entry in the database.
  13. *
  14. * Outside of the .install file we would use drupal_write_record() to populate
  15. * the database, but it cannot be used here, so we'll use db_insert().
  16. *
  17. * @see hook_install()
  18. *
  19. * @ingroup dbtng_example
  20. */
  21. function dbtng_example_install() {
  22. // Add a default entry.
  23. $fields = array(
  24. 'name' => 'John',
  25. 'surname' => 'Doe',
  26. 'age' => 0,
  27. );
  28. db_insert('dbtng_example')
  29. ->fields($fields)
  30. ->execute();
  31. // Add another entry.
  32. $fields = array(
  33. 'name' => 'John',
  34. 'surname' => 'Roe',
  35. 'age' => 100,
  36. 'uid' => 1,
  37. );
  38. db_insert('dbtng_example')
  39. ->fields($fields)
  40. ->execute();
  41. }
  42. /**
  43. * Implements hook_schema().
  44. *
  45. * Defines the database tables used by this module.
  46. * Remember that the easiest way to create the code for hook_schema is with
  47. * the @link http://drupal.org/project/schema schema module @endlink
  48. *
  49. * @see hook_schema()
  50. * @ingroup dbtng_example
  51. */
  52. function dbtng_example_schema() {
  53. $schema['dbtng_example'] = array(
  54. 'description' => 'Stores example person entries for demonstration purposes.',
  55. 'fields' => array(
  56. 'pid' => array(
  57. 'type' => 'serial',
  58. 'not null' => TRUE,
  59. 'description' => 'Primary Key: Unique person ID.',
  60. ),
  61. 'uid' => array(
  62. 'type' => 'int',
  63. 'not null' => TRUE,
  64. 'default' => 0,
  65. 'description' => "Creator user's {users}.uid",
  66. ),
  67. 'name' => array(
  68. 'type' => 'varchar',
  69. 'length' => 255,
  70. 'not null' => TRUE,
  71. 'default' => '',
  72. 'description' => 'Name of the person.',
  73. ),
  74. 'surname' => array(
  75. 'type' => 'varchar',
  76. 'length' => 255,
  77. 'not null' => TRUE,
  78. 'default' => '',
  79. 'description' => 'Surname of the person.',
  80. ),
  81. 'age' => array(
  82. 'type' => 'int',
  83. 'not null' => TRUE,
  84. 'default' => 0,
  85. 'size' => 'tiny',
  86. 'description' => 'The age of the person in years.',
  87. ),
  88. ),
  89. 'primary key' => array('pid'),
  90. 'indexes' => array(
  91. 'name' => array('name'),
  92. 'surname' => array('surname'),
  93. 'age' => array('age'),
  94. ),
  95. );
  96. return $schema;
  97. }