tripal_chado.entity.api.inc 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. <?php
  2. /**
  3. * @file
  4. * Provides an application programming interface (API) to manage entities
  5. * that use Chado as their base data.
  6. */
  7. /**
  8. * @defgroup tripal_chado_entity_api Chado Entity
  9. * @ingroup tripal_chado_api
  10. * @{
  11. * Provides an application programming interface (API) to manage entities
  12. * that use Chado as their base data.
  13. * @}
  14. */
  15. /**
  16. * Retreive the entity_id assigned to a given record_id and bundle.
  17. *
  18. * @param $bundle
  19. * A bundle object (as retrieved from tripal_load_bundle_entity().
  20. * @param $record_id
  21. * The ID of the record in the Chado table. The record must belong to
  22. * the table to which the bundle is associated in chado.
  23. *
  24. * @return
  25. * The ID of the entity that belongs to the given record_id.
  26. *
  27. * @ingroup tripal_chado_entity_api
  28. */
  29. function chado_get_record_entity_by_bundle(TripalBundle $bundle, $record_id) {
  30. if (!$bundle) {
  31. throw new Exception('Please provide a TripalBundle object.');
  32. };
  33. if (!$record_id) {
  34. throw new Exception('Please provide an integer record ID.');
  35. };
  36. if (!is_numeric($record_id)) {
  37. throw new Exception('Please provide an integer record ID. The value provided was "' . $record_id . '"');
  38. }
  39. $chado_entity_table = chado_get_bundle_entity_table($bundle);
  40. return db_select($chado_entity_table, 'CE')
  41. ->fields('CE', array('entity_id'))
  42. ->condition('CE.record_id', $record_id)
  43. ->execute()
  44. ->fetchField();
  45. }
  46. /**
  47. * Retreive the entity_id assigned to a given record_id and base table.
  48. *
  49. * @param $data_table
  50. * The name of the Chado base table.
  51. * @param $record_id
  52. * The ID of the record in the Chado table. The record must belong to
  53. * the table to which the bundle is associated in chado.
  54. *
  55. * @return
  56. * The ID of the entity that belongs to the given record_id, or NULL
  57. * otherwise.
  58. *
  59. * @ingroup tripal_chado_entity_api
  60. */
  61. function chado_get_record_entity_by_table($data_table, $record_id) {
  62. // The data table and type_id are required.
  63. if (!$data_table) {
  64. throw new Exception('Please provide the $data_table argument.');
  65. };
  66. if (!$record_id) {
  67. throw new Exception('Please provide an integer record ID.');
  68. };
  69. if (!is_numeric($record_id)) {
  70. throw new Exception('Please provide an integer record ID. The value provided was "' . $record_id . '"');
  71. }
  72. // Get the list of bundles for this table.
  73. $bundles = db_select('chado_bundle', 'CB')
  74. ->fields('CB', array('bundle_id'))
  75. ->condition('CB.data_table', $data_table)
  76. ->execute();
  77. // Look for the record ID in the appropriate chado table.
  78. while ($bundle_id = $bundles->fetchField()) {
  79. $entity_id = db_select('chado_bio_data_' . $bundle_id , 'CBD')
  80. ->fields('CBD', array('entity_id'))
  81. ->condition('record_id', $record_id)
  82. ->execute()
  83. ->fetchField();
  84. if ($entity_id) {
  85. return $entity_id;
  86. }
  87. }
  88. return NULL;
  89. }
  90. /**
  91. * A helper function that provides the Chado mapping table for the bundle.
  92. *
  93. * The tripal_chado module must map entities to their corresponding record
  94. * in Chado. Each bundl type has their own table for this mapping. This
  95. * function provides the name of the table given the bundle name. A mapping
  96. * table will only map to one table in Chado so the record_id's of the mapping
  97. * table should always be unique.
  98. *
  99. * @param $bundle
  100. * A bundle object (as retrieved from tripal_load_bundle_entity().
  101. *
  102. * @return
  103. * The name of the mapping table that Chado uses to map entities to records.
  104. *
  105. * @ingroup tripal_chado_entity_api
  106. */
  107. function chado_get_bundle_entity_table($bundle) {
  108. if (!$bundle) {
  109. return '';
  110. }
  111. return 'chado_' . $bundle->name;
  112. }