Source for file StorableRecordSet.class.php

Documentation is available at StorableRecordSet.class.php

  1. <?php
  2.  
  3. require_once(HARMONI."dataManager/record/RecordSet.class.php");
  4.  
  5. /**
  6. * The StorableRecordSet allows you to store a {@link RecordSet} in the database.
  7. *
  8. * @package harmoni.datamanager
  9. *
  10. * @copyright Copyright &copy; 2005, Middlebury College
  11. * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License (GPL)
  12. *
  13. * @version $Id: StorableRecordSet.class.php,v 1.12 2007/09/04 20:25:32 adamfranco Exp $
  14. */
  15. class StorableRecordSet extends RecordSet {
  16. var $_fetchedIDs;
  17. var $_storedRecordIDs;
  18. var $_fetchMode;
  19. var $_myID;
  20. function StorableRecordSet($id) {
  21. parent::RecordSet();
  22. $this->_myID = $id;
  23. $this->_fetchMode = -1;
  24. $this->_fetchedIDs = array();
  25. $this->_storedRecordIDs = array();
  26. }
  27. /**
  28. * Takes an associative array of columns from a database query and populates internal variables.
  29. * @param ref array $row
  30. * @return void
  31. */
  32. function takeRow($row) {
  33. if (in_array($row["fk_record"], $this->_fetchedIDs)) return;
  34. $recordManager = Services::getService("RecordManager");
  35. $this->_storedRecordIDs[] = $row["fk_record"];
  36. }
  37.  
  38. /**
  39. * Fetches all the {@link Record}s that we know about.
  40. * Use {@link RecordSet::getRecords()} to get the fetched records.
  41. * @param optional int $mode Defaults to RECORD_CURRENT. Specifies the fetch mode. Must be one of the RECORD_* constants.
  42. * @param optional mixed $limitResults NOT IMPLEMENTED.
  43. * @return void
  44. */
  45. function loadRecords($mode=RECORD_CURRENT, $limitResults = null) {
  46. $recordManager = Services::getService("RecordManager");
  47. $records =$recordManager->fetchRecords($this->getRecordIDs(), $mode, $limitResults);
  48. // Cycle through the resulting records to put them in an indexed array
  49. if (!is_array($this->_records)) $this->_records = array();
  50. foreach (array_keys($records) as $key) {
  51. $id = $records[$key]->getID();
  52. // if (in_array($id, $this->_fetchedIDs)) continue;
  53. if ($this->getRecordByID($id)==null) $this->_records[] =$records[$key];
  54. if (!in_array($id, $this->_fetchedIDs)) $this->_fetchedIDs[] = $id;
  55. }
  56.  
  57. $this->_fetchMode = $mode;
  58. }
  59. /**
  60. * Returns an array of Record IDs that we know about.
  61. * @return array
  62. */
  63. function getRecordIDs() {
  64. return array_unique(array_merge($this->_storedRecordIDs, parent::getRecordIDs()));
  65. }
  66. /**
  67. * Returns our ID.
  68. * @access public
  69. * @return int
  70. */
  71. function getID()
  72. {
  73. return $this->_myID;
  74. }
  75. /**
  76. * Removes the given record from the set.
  77. * @param ref object $record A {@link Record} object.
  78. * @access public
  79. * @return void
  80. */
  81. function removeRecord($record)
  82. {
  83. $id = $record->getID();
  84. parent::removeRecord($record);
  85. // now remove the $id from our internal list, if it's there.
  86. if (in_array($id, $this->_storedRecordIDs)) {
  87. $new = array();
  88. foreach ($this->_storedRecordIDs as $rID) {
  89. if ($id != $rID) $new[] = $rID;
  90. }
  91. $this->_storedRecordIDs = $new;
  92. }
  93. // and this list, to avoid messing up numRecords().
  94. if (in_array($id, $this->_fetchedIDs)) {
  95. $new = array();
  96. foreach ($this->_fetchedIDs as $rID) {
  97. if ($id != $rID) $new[] = $rID;
  98. }
  99. $this->_fetchedIDs = $new;
  100. }
  101. }
  102. /**
  103. * Attempts to commit our {@link Record}s to the database and update our mapping.
  104. * @param boolean optional $ignoreMandatory If true, doesn't fail if mandatory
  105. * fields don't have values.
  106. * @return void
  107. */
  108. function commit($ignoreMandatory=false) {
  109. $ids = array();
  110. if (count($this->_records)) {
  111. for($i=0; $i<count($this->_records); $i++) {
  112. $this->_records[$i]->commit($ignoreMandatory);
  113. $ids[] = $this->_records[$i]->getID();
  114. }
  115. $this->_records = array();
  116. $this->_fetchMode = -1;
  117. }
  118. if ($this->_dirty) {
  119. // syncrhonize the database
  120. $ids = array_merge($ids, $this->_storedRecordIDs);
  121. // Make sure that we only have one ID for each record.
  122. $ids = array_unique($ids);
  123.  
  124. $dbHandler = Services::getService("DatabaseManager");
  125. // first delete all the old mappings
  126. $query = new DeleteQuery;
  127. $query->setTable("dm_record_set");
  128. $query->setWhere("dm_record_set.id='".addslashes($this->_myID)."'");
  129. // printpre(MySQL_SQLGenerator::generateSQLQuery($query));
  130. $dbHandler->query($query, DATAMANAGER_DBID);
  131. if (count($ids)) {
  132. // next insert all our mappings back in.
  133. $query = new InsertQuery;
  134. $query->setTable("dm_record_set");
  135. $query->setColumns(array("id","fk_record"));
  136. foreach ($ids as $id) {
  137. $query->addRowOfValues(array("'".addslashes($this->_myID)."'", "'".addslashes($id)."'"));
  138. if (!in_array($id, $this->_storedRecordIDs)) $this->_storedRecordIDs[] = $id;
  139. }
  140. // printpre(MySQL_SQLGenerator::generateSQLQuery($query));
  141. $dbHandler->query($query,DATAMANAGER_DBID);
  142. // done!
  143. }
  144. }
  145. }
  146. /**
  147. * Adds the given ID to our list of Records.
  148. * @param integer $id
  149. * @return void
  150. */
  151. function addRecordID( $id ) {
  152. ArgumentValidator::validate($id, IntegerValidatorRule::getRule());
  153. $this->_dirty = true;
  154. $recordManager = Services::getService("RecordManager");
  155. $this->_records[] =$recordManager->fetchRecord( $id );
  156. $this->_storedRecordIDs[] = $id;
  157. }
  158. /**
  159. * Remove a record by ID.
  160. * @param int $id
  161. * @access public
  162. * @return void
  163. */
  164. function removeRecordByID($id)
  165. {
  166. $record =$this->getRecordByID($id);
  167. if ($record) $this->removeRecord($record);
  168. }
  169. /**
  170. * Returns a {@link Record} by ID.
  171. * @param int $id
  172. * @access public
  173. * @return ref object
  174. */
  175. function getRecordByID($id)
  176. {
  177. for ($i = 0; $i < count($this->_records); $i++) {
  178. if ($this->_records[$i]->getID() == $id) return $this->_records[$i];
  179. }
  180. if (in_array($id, $this->_storedRecordIDs)) {
  181. $mgr = Services::getService("RecordManager");
  182. $record =$mgr->fetchRecord($id);
  183. $this->_fetchedIDs[] = $id;
  184. $this->_records[] =$record;
  185. return $record;
  186. }
  187. return ($null=null);
  188. }
  189. /**
  190. * Returns all of the {@link Record}s within this Set to the state they were in on the given date.
  191. * @param ref object $date A {@link DateAndTime} object.
  192. * @access public
  193. * @return boolean
  194. */
  195. function revertToDate($date)
  196. {
  197. $this->loadRecords(RECORD_FULL);
  198. parent::revertToDate($date);
  199. }
  200. }
  201.  
  202. ?>

Documentation generated on Wed, 19 Sep 2007 10:26:48 -0400 by phpDocumentor 1.3.0RC3