tripal_chado.property.api.inc 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629
  1. <?php
  2. /**
  3. * @file
  4. * Provides an application programming interface (API) to manage data withing the Chado database.
  5. */
  6. /**
  7. * @defgroup tripal_chado_api Chado API
  8. * @ingroup tripal_chado_api
  9. * @{
  10. * Provides an application programming interface (API) to manage data withing the Chado database.
  11. *
  12. * This includes functions for selecting, inserting, updating and deleting records
  13. * in Chado tables. The functions will ensure proper integrity contraints are met
  14. * for inserts and updates.
  15. *
  16. * Also, a set of functions is provided for creating template variables. First,
  17. * is the chado_generate_var which is used to select one ore more
  18. * records from a table and return an array with foreign key relationships fully
  19. * populated. For example, if selecting a feature, the organism_id and type_id
  20. * would be present in the returned array as a nested array with their respective
  21. * foreign keys also nested. The only fields that are not included are text
  22. * fields (which may be very large) or many-to-many foreign key relationships.
  23. * However, these fields and relationships can be expanded using the
  24. * chado_expand_var.
  25. *
  26. * When a row from a chado table is selected using these two functions, it provides
  27. * a way for users who want to cutomize Drupal template files to access all data
  28. * associate with a specific record.
  29. *
  30. * Finally, the property tables in Chado generally follow the same format. Therefore
  31. * there is a set of functions for inserting, updating and deleting properties for
  32. * any table. This provides quick lookup of properties (provided the CV term is
  33. * known).
  34. *
  35. * @}
  36. *
  37. */
  38. /**
  39. * Retrieve a property for a given base table record.
  40. *
  41. * @param $record
  42. * An array used to identify the record to which the property is associated.
  43. * The following keys must be used:
  44. * -table: The base table for which the property should be updated.
  45. * Thus to update a property for a feature the base_table=feature.
  46. * -id: The primary key value of the base table. The property will be
  47. * associated with the record that matches this id.
  48. * -prop_id: The primary key in the [table]prop table. If this value
  49. * is supplied then the 'table' and 'id' keys are not needed.
  50. * @param $property
  51. * An associative array used to specify the property to be selected. It can
  52. * contain the following keys. The keys must be specified to uniquely identify
  53. * the term to be applied. If the options identify more than one CV term
  54. * then an error will occur.
  55. * -type_name: The cvterm name to be selected.
  56. * -type_id: The cvterm_id of the term to be selected.
  57. * -cv_id: The cv_id of the CV that contains the term.
  58. * -cv_name: The name of the CV that contains the term.
  59. * -value: The specific value for the property.
  60. * -rank: The specific rank for the property.
  61. * @return
  62. * An array in the same format as that generated by the function
  63. * chado_generate_var(). If only one record is returned it
  64. * is a single object. If more than one record is returned then it is an array
  65. * of objects
  66. *
  67. * @ingroup tripal_chado_api
  68. */
  69. function chado_get_property($record, $property) {
  70. $base_table = array_key_exists('table', $record) ? $record['table'] : '';
  71. $base_id = array_key_exists('id', $record) ? $record['id'] : '';
  72. $prop_id = array_key_exists('prop_id', $record) ? $record['prop_id'] : '';
  73. $type_name = array_key_exists('type_name', $property) ? $property['type_name'] : '';
  74. $type_id = array_key_exists('type_id', $property) ? $property['type_id'] : '';
  75. $cv_name = array_key_exists('cv_name', $property) ? $property['cv_name'] : '';
  76. $cv_id = array_key_exists('cv_id', $property) ? $property['cv_id'] : '';
  77. $value = array_key_exists('value', $property) ? $property['value'] : '';
  78. $rank = array_key_exists('rank', $property) ? $property['rank'] : 0;
  79. // Build the values array for checking if the CVterm exists and for
  80. // retrieving the term as a property.
  81. $type = array();
  82. if ($cv_id) {
  83. $type['cv_id'] = $cv_id;
  84. }
  85. if ($cv_name) {
  86. $type['cv_id'] = array(
  87. 'name' => $cv_name,
  88. );
  89. }
  90. if ($type_name) {
  91. $type['name'] = $type_name;
  92. }
  93. if ($type_id) {
  94. $type['cvterm_id'] = $type_id;
  95. }
  96. // Make sure the CV term exists;
  97. $options = array();
  98. $term = chado_select_record('cvterm', array('cvterm_id'), $type, $options);
  99. if (!$term or count($term) == 0) {
  100. tripal_report_error('tripal_chado', TRIPAL_ERROR, "chado_get_property: " .
  101. "Cannot find the term described by: %property.",
  102. array('%property' => print_r($property, TRUE)));
  103. return FALSE;
  104. }
  105. if (count($term) > 1) {
  106. tripal_report_error('tripal_chado', TRIPAL_ERROR, "chado_get_property: " .
  107. "Multiple terms found. Cannot add the property. Property was described " .
  108. "by: %property.",
  109. array('%property' => print_r($property, TRUE))); return FALSE;
  110. }
  111. // get the foreign key for this property table
  112. $table_desc = chado_get_schema($base_table . 'prop');
  113. $fkcol = key($table_desc['foreign keys'][$base_table]['columns']);
  114. // construct the array of values to be selected
  115. $values = array(
  116. $fkcol => $base_id,
  117. 'type_id' => $type,
  118. );
  119. // if we have the unique property_id make sure to add that to the values
  120. if ($prop_id) {
  121. $property_pkey = $table_desc['primary key'][0];
  122. $values[$property_pkey] = $prop_id;
  123. }
  124. $results = chado_generate_var($base_table . 'prop', $values);
  125. if ($results) {
  126. $results = chado_expand_var($results, 'field', $base_table . 'prop.value');
  127. }
  128. return $results;
  129. }
  130. /**
  131. * Insert a property for a given base table.
  132. *
  133. * By default if the property already exists a new property is added with the
  134. * next available rank. If the option 'update_if_present' is specified then
  135. * the record will be updated if it exists rather than adding a new property.
  136. *
  137. * @param $record
  138. * An associative array used to identify the record to which the property
  139. * should be assigned. The following keys must be used:
  140. * -table: The base table for which the property should be inserted.
  141. * Thus to insert a property for a feature the base_table=feature and
  142. * property is inserted into featureprop
  143. * -id: The primary key value of the base table. The property will be
  144. * associated with the record that matches this id.
  145. * @param $property
  146. * An associative array used to specify the property to be added. It can
  147. * contain the following keys. The keys must be specified to uniquely identify
  148. * the term to be applied. If the options identify more than one CV term
  149. * then an error will occur.
  150. * -type_name: The cvterm name to be selected.
  151. * -type_id: The cvterm_id of the term to be selected.
  152. * -cv_id: The cv_id of the CV that contains the term.
  153. * -cv_name: The name of the CV that contains the term.
  154. * -value: The specific value for the property.
  155. * -rank: The specific rank for the property.
  156. * @param $options
  157. * An associative array containing the following keys:
  158. * -update_if_present: A boolean indicating whether an existing record
  159. * should be updated. If the property already exists and this value is
  160. * not specified or is zero then a new property will be added with the
  161. * next largest rank.
  162. * -force_rank: If the specified rank is already used by another property
  163. * recrod for the same base_id, then set force_rank to TRUE to require
  164. * that only the specified rank can be used. Otherwise, the next
  165. * available rank will be used. If 'update_if_present' is FALSE and
  166. * 'force_rank' is set then an error will occur.
  167. *
  168. * @return
  169. * Return TRUE if successful and FALSE otherwise
  170. *
  171. * @ingroup tripal_chado_api
  172. */
  173. function chado_insert_property($record, $property, $options = array()) {
  174. $base_table = array_key_exists('table', $record) ? $record['table'] : '';
  175. $base_id = array_key_exists('id', $record) ? $record['id'] : '';
  176. $type_name = array_key_exists('type_name', $property) ? $property['type_name'] : '';
  177. $type_id = array_key_exists('type_id', $property) ? $property['type_id'] : '';
  178. $cv_name = array_key_exists('cv_name', $property) ? $property['cv_name'] : '';
  179. $cv_id = array_key_exists('cv_id', $property) ? $property['cv_id'] : '';
  180. $value = array_key_exists('value', $property) ? $property['value'] : '';
  181. $rank = array_key_exists('rank', $property) ? $property['rank'] : 0;
  182. $update_if_present = array_key_exists('update_if_present', $options) ? $options['update_if_present'] : FALSE;
  183. $force_rank = array_key_exists('force_rank', $options) ? $options['force_rank'] : FALSE;
  184. // First see if the property is already assigned to the record. I
  185. $props = chado_get_property($record, $property);
  186. if (!is_array($props)) {
  187. if ($props) {
  188. $props = array($props);
  189. }
  190. else {
  191. $props = array();
  192. }
  193. }
  194. if (count($props) > 0) {
  195. // The property is already assigned, so, see if we should update it.
  196. if ($update_if_present) {
  197. return chado_update_property($record, $property);
  198. }
  199. else {
  200. if (!$force_rank) {
  201. // iterate through the properties returned and check to see if the
  202. // property with this value already exists if not, get the largest rank
  203. // and insert the same property but with this new value
  204. foreach ($props as $prop) {
  205. if ($prop->rank > $rank) {
  206. $rank = $prop->rank;
  207. }
  208. if (strcmp($prop->value, $value) == 0) {
  209. return TRUE;
  210. }
  211. }
  212. // now add 1 to the rank
  213. $rank++;
  214. }
  215. else {
  216. tripal_report_error('tripal_chado', TRIPAL_ERROR, "chado_insert_property: " .
  217. "The property is already assigned to the record with the following " .
  218. "rank. And, because the 'force_rank' option is used, the property " .
  219. "cannot be added: %property.",
  220. array('%property' => print_r($property, TRUE)));
  221. return FALSE;
  222. }
  223. }
  224. }
  225. // Build the values array for checking if the CVterm exists and for
  226. // inserting the term as a property.
  227. $values = array();
  228. if ($cv_id) {
  229. $values['cv_id'] = $cv_id;
  230. }
  231. if ($cv_name) {
  232. $values['cv_id'] = array(
  233. 'name' => $cv_name,
  234. );
  235. }
  236. if ($type_name) {
  237. $values['name'] = $type_name;
  238. }
  239. if ($type_id) {
  240. $values['cvterm_id'] = $type_id;
  241. }
  242. // Make sure the CV term exists;
  243. $options = array();
  244. $term = chado_select_record('cvterm', array('cvterm_id'), $values, $options);
  245. if (!$term or count($term) == 0) {
  246. tripal_report_error('tripal_chado', TRIPAL_ERROR, "chado_insert_property: " .
  247. "Cannot find the term described by: %property.",
  248. array('%property' => print_r($property, TRUE)));
  249. return FALSE;
  250. }
  251. if (count($term) > 1) {
  252. tripal_report_error('tripal_chado', TRIPAL_ERROR, "chado_insert_property: " .
  253. "Multiple terms found. Cannot add the property. Property was described " .
  254. "by: %property.",
  255. array('%property' => print_r($property, TRUE))); return FALSE;
  256. }
  257. // Get the foreign key for this property table
  258. $table_desc = chado_get_schema($base_table . 'prop');
  259. $fkcol = key($table_desc['foreign keys'][$base_table]['columns']);
  260. // Add the property to the record.
  261. $values = array(
  262. $fkcol => $base_id,
  263. 'type_id' => $values,
  264. 'value' => $value,
  265. 'rank' => $rank,
  266. );
  267. $result = chado_insert_record($base_table . 'prop', $values);
  268. return $result;
  269. }
  270. /**
  271. * Update a property for a given base table record and property name.
  272. *
  273. * @param $record
  274. * An associative array used to identify the record to which the property
  275. * should be updated. The following keys must be used:
  276. * -table: The base table for which the property should be updated.
  277. * Thus to update a property for a feature the base_table=feature.
  278. * -id: The primary key value of the base table. The property will be
  279. * associated with the record that matches this id.
  280. * -prop_id: The primary key in the [table]prop table. If this value
  281. * is supplied then the 'table' and 'id' keys are not needed.
  282. * @param $property
  283. * An associative array used to specify the property to be updated. It can
  284. * contain the following keys. The keys must be specified to uniquely identify
  285. * the term to be applied. If the options identify more than one CV term
  286. * then an error will occur.
  287. * -type_name: The cvterm name to be selected.
  288. * -type_id: The cvterm_id of the term to be selected.
  289. * -cv_id: The cv_id of the CV that contains the term.
  290. * -cv_name: The name of the CV that contains the term.
  291. * -value: The specific value for the property.
  292. * -rank: The specific rank for the property.
  293. * @param $options
  294. * An associative array containing the following keys:
  295. * -insert_if_missing: A boolean indicating whether a record should be
  296. * inserted if one doesn't exist to update.
  297. *
  298. *
  299. * @return
  300. * Return TRUE on Update/Insert and FALSE otherwise
  301. *
  302. * @ingroup tripal_chado_api
  303. */
  304. function chado_update_property($record, $property, $options = array()) {
  305. $base_table = array_key_exists('table', $record) ? $record['table'] : '';
  306. $base_id = array_key_exists('id', $record) ? $record['id'] : '';
  307. $prop_id = array_key_exists('prop_id', $record) ? $record['prop_id'] : '';
  308. $type_name = array_key_exists('type_name', $property) ? $property['type_name'] : '';
  309. $type_id = array_key_exists('type_id', $property) ? $property['type_id'] : '';
  310. $cv_name = array_key_exists('cv_name', $property) ? $property['cv_name'] : '';
  311. $cv_id = array_key_exists('cv_id', $property) ? $property['cv_id'] : '';
  312. $value = array_key_exists('value', $property) ? $property['value'] : '';
  313. $rank = array_key_exists('rank', $property) ? $property['rank'] : 0;
  314. $insert_if_missing = array_key_exists('insert_if_missing', $options) ? $options['insert_if_missing'] : FALSE;
  315. // first see if the property is missing (we can't update a missing property
  316. $prop = chado_get_property($record, $property);
  317. if (count($prop) == 0) {
  318. if ($insert_if_missing) {
  319. return chado_insert_property($record, $property);
  320. }
  321. else {
  322. return FALSE;
  323. }
  324. }
  325. // Build the values array for checking if the CVterm exists.
  326. $type = array();
  327. if ($cv_id) {
  328. $type['cv_id'] = $cv_id;
  329. }
  330. if ($cv_name) {
  331. $type['cv_id'] = array(
  332. 'name' => $cv_name,
  333. );
  334. }
  335. if ($type_name) {
  336. $type['name'] = $type_name;
  337. }
  338. if ($type_id) {
  339. $type['cvterm_id'] = $type_id;
  340. }
  341. // Make sure the CV term exists;
  342. $options = array();
  343. $term = chado_select_record('cvterm', array('cvterm_id'), $type, $options);
  344. if (!$term or count($term) == 0) {
  345. tripal_report_error('tripal_chado', TRIPAL_ERROR, "chado_update_property: " .
  346. "Cannot find the term described by: %property.",
  347. array('%property' => print_r($property, TRUE)));
  348. return FALSE;
  349. }
  350. if (count($term) > 1) {
  351. tripal_report_error('tripal_chado', TRIPAL_ERROR, "chado_update_property: " .
  352. "Multiple terms found. Cannot add the property. Property was described " .
  353. "by: %property.",
  354. array('%property' => print_r($property, TRUE)));
  355. return FALSE;
  356. }
  357. // Get the foreign key for this property table
  358. $table_desc = chado_get_schema($base_table . 'prop');
  359. $fkcol = key($table_desc['foreign keys'][$base_table]['columns']);
  360. // construct the array that will match the exact record to update
  361. $match = array(
  362. $fkcol => $base_id,
  363. 'type_id' => $type,
  364. );
  365. // If we have the unique property_id, make sure to use it in the match to
  366. // ensure we get the exact record. Doesn't rely on there only being one
  367. // property of that type.
  368. if ($prop_id) {
  369. $property_pkey = $table_desc['primary key'][0];
  370. $match = array(
  371. $property_pkey => $prop_id,
  372. );
  373. }
  374. // Construct the array of values to be updated.
  375. $values = array();
  376. $values['value'] = $value;
  377. if ($rank) {
  378. $values['rank'] = $rank;
  379. }
  380. // If we have the unique property_id then we can also update the type
  381. // thus add it to the values to be updated
  382. if ($prop_id) {
  383. $values['type_id'] = $type;
  384. }
  385. return chado_update_record($base_table . 'prop', $match, $values);
  386. }
  387. /**
  388. * Deletes a property for a given base table record using the property name
  389. *
  390. * @param $record
  391. * An associative array used to identify the record to which the property
  392. * should be deleted. The following keys must be used:
  393. * -table: The base table for which the property should be deleted.
  394. * Thus to update a property for a feature the base_table=feature.
  395. * -id: The primary key value of the base table. The property will be
  396. * deleted from the record that matches this id.
  397. * -prop_id: The primary key in the [table]prop table to be deleted. If
  398. * this value is supplied then the 'table' and 'id' keys are not needed.
  399. * @param $property
  400. * An associative array used to specify the property to be updated. It can
  401. * contain the following keys. The keys must be specified to uniquely identify
  402. * the term to be applied. If the options identify more than one CV term
  403. * then an error will occur.
  404. * -type_name: The cvterm name to be selected.
  405. * -type_id: The cvterm_id of the term to be selected.
  406. * -cv_id: The cv_id of the CV that contains the term.
  407. * -cv_name: The name of the CV that contains the term.
  408. * -value: The specific value for the property.
  409. * -rank: The specific rank for the property.
  410. *
  411. * @return
  412. * Return TRUE on successful deletion and FALSE otherwise
  413. *
  414. * @ingroup tripal_chado_api
  415. */
  416. function chado_delete_property($record, $property) {
  417. $base_table = array_key_exists('table', $record) ? $record['table'] : '';
  418. $base_id = array_key_exists('id', $record) ? $record['id'] : '';
  419. $prop_id = array_key_exists('prop_id', $record) ? $record['prop_id'] : '';
  420. $type_name = array_key_exists('type_name', $property) ? $property['type_name'] : '';
  421. $type_id = array_key_exists('type_id', $property) ? $property['type_id'] : '';
  422. $cv_name = array_key_exists('cv_name', $property) ? $property['cv_name'] : '';
  423. $cv_id = array_key_exists('cv_id', $property) ? $property['cv_id'] : '';
  424. $value = array_key_exists('value', $property) ? $property['value'] : '';
  425. $rank = array_key_exists('rank', $property) ? $property['rank'] : 0;
  426. // Build the values array for checking if the CVterm exists
  427. $type = array();
  428. if ($cv_id) {
  429. $type['cv_id'] = $cv_id;
  430. }
  431. if ($cv_name) {
  432. $type['cv_id'] = array(
  433. 'name' => $cv_name,
  434. );
  435. }
  436. if ($type_name) {
  437. $type['name'] = $type_name;
  438. }
  439. if ($type_id) {
  440. $type['cvterm_id'] = $type_id;
  441. }
  442. // Make sure the CV term exists;
  443. $options = array();
  444. $term = chado_select_record('cvterm', array('cvterm_id'), $type, $options);
  445. if (!$term or count($term) == 0) {
  446. tripal_report_error('tripal_chado', TRIPAL_ERROR, "chado_delete_property: " .
  447. "Cannot find the term described by: %property.",
  448. array('%property' => print_r($property, TRUE)));
  449. return FALSE;
  450. }
  451. if (count($term) > 1) {
  452. tripal_report_error('tripal_chado', TRIPAL_ERROR, "chado_delete_property: " .
  453. "Multiple terms found. Cannot add the property. Property was described " .
  454. "by: %property.",
  455. array('%property' => print_r($property, TRUE))); return FALSE;
  456. }
  457. // get the foreign key for this property table
  458. $table_desc = chado_get_schema($base_table . 'prop');
  459. $fkcol = key($table_desc['foreign keys'][$base_table]['columns']);
  460. // If we have the unique property_id, make sure to use it in the match to ensure
  461. // we get the exact record. Doesn't rely on there only being one property of that type
  462. if ($prop_id) {
  463. $property_pkey = $table_desc['primary key'][0];
  464. $match = array(
  465. $property_pkey => $prop_id
  466. );
  467. }
  468. // construct the array that will match the exact record to update
  469. else {
  470. $match = array(
  471. $fkcol => $record_id,
  472. 'type_id' => $type,
  473. );
  474. }
  475. return chado_delete_record($base_table . 'prop', $match);
  476. }
  477. /**
  478. * Get all records in the base table assigned one or more properties.
  479. *
  480. * The property or properties of interest are specified using the $property
  481. * argument.
  482. *
  483. * @param $record
  484. * An associative array used to identify the table and subset of records to
  485. * to be searched:
  486. * -table: The base table for which the property should be updated.
  487. * Thus to update a property for a feature the base_table=feature.
  488. * -base_records: An array in the format accepted by the chado_select_record
  489. * for specifying a subset of records in the base table.
  490. * @param $property
  491. * An associative array used to specify the property to be selected for. It
  492. * can contain the following keys. The keys must be specified to uniquely
  493. * identify the term to be searched. If the options identify more than one
  494. * CV term then an error will occur.
  495. * -type_name: The cvterm name to be selected.
  496. * -type_id: The cvterm_id of the term to be selected.
  497. * -cv_id: The cv_id of the CV that contains the term.
  498. * -cv_name: The name of the CV that contains the term.
  499. * -value: The specific value for the property.
  500. * -rank: The specific rank for the property.
  501. * @param $options
  502. * An array of options supported by chado_generate_var(). These keys
  503. * are used for generating the cvterm objects returned by this function.
  504. *
  505. * @return
  506. * An array of chado variables with the given property
  507. *
  508. * @ingroup tripal_chado_api
  509. */
  510. function chado_get_record_with_property($record, $property, $options = array()) {
  511. $base_table = array_key_exists('table', $record) ? $record['table'] : '';
  512. $base_records= array_key_exists('base_records', $record) ? $record['base_records'] : array();
  513. $type_name = array_key_exists('type_name', $property) ? $property['type_name'] : '';
  514. $type_id = array_key_exists('type_id', $property) ? $property['type_id'] : '';
  515. $cv_name = array_key_exists('cv_name', $property) ? $property['cv_name'] : '';
  516. $cv_id = array_key_exists('cv_id', $property) ? $property['cv_id'] : '';
  517. $value = array_key_exists('value', $property) ? $property['value'] : '';
  518. $rank = array_key_exists('rank', $property) ? $property['rank'] : '';
  519. $property_table = $base_table . 'prop';
  520. $foreignkey_name = $base_table . '_id';
  521. // Build the values array for checking if the CVterm exists and for
  522. // inserting the term as a property.
  523. $type = array();
  524. if ($cv_id) {
  525. $type['cv_id'] = $cv_id;
  526. }
  527. if ($cv_name) {
  528. $type['cv_id'] = array(
  529. 'name' => $cv_name,
  530. );
  531. }
  532. if ($type_name) {
  533. $type['name'] = $type_name;
  534. }
  535. if ($type_id) {
  536. $type['cvterm_id'] = $type_id;
  537. }
  538. // Make sure the CV term exists;
  539. $term = chado_select_record('cvterm', array('cvterm_id'), $type);
  540. if (!$term or count($term) == 0) {
  541. tripal_report_error('tripal_chado', TRIPAL_ERROR, "chado_update_property: " .
  542. "Cannot find the term described by: %property.",
  543. array('%property' => print_r($property, TRUE)));
  544. return FALSE;
  545. }
  546. if (count($term) > 1) {
  547. tripal_report_error('tripal_chado', TRIPAL_ERROR, "chado_update_property: " .
  548. "Multiple terms found. Cannot add the property. Property was described " .
  549. "by: %property.",
  550. array('%property' => print_r($property, TRUE))); return FALSE;
  551. }
  552. // Build the array for identifying the property.
  553. $values = array();
  554. $values['type_id'] = $type;
  555. if ($rank) {
  556. $values['rank'] = $rank;
  557. }
  558. if ($value) {
  559. $values['value'] = $value;
  560. }
  561. // Add the base records details to the values array.
  562. if (!empty($base_records)) {
  563. $values[$foreignkey_name] = $base_records;
  564. }
  565. // Now select the ids of the base table that have the properties we want that match.
  566. $select = chado_select_record($property_table, array($foreignkey_name), $values);
  567. // For each of these ids, pull out the full base records
  568. $records = array();
  569. foreach ($select as $s) {
  570. $id = $s->{$foreignkey_name};
  571. $values = array($foreignkey_name => $id);
  572. $records[$id] = chado_generate_var($base_table, $values, $options);
  573. }
  574. return $records;
  575. }