tripal_chado.entity.api.inc 3.1 KB

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