dbobjectset.class.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. <?php
  2. /**
  3. * A set of persistent objects, could be heterogeneous
  4. *
  5. * @package iTopORM
  6. * @author Romain Quetiez <romainquetiez@yahoo.fr>
  7. * @author Denis Flaven <denisflave@free.fr>
  8. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
  9. * @link www.itop.com
  10. * @since 1.0
  11. * @version 1.1.1.1 $
  12. */
  13. class DBObjectSet
  14. {
  15. private $m_oFilter;
  16. private $m_aOrderBy;
  17. public $m_bLoaded;
  18. private $m_aData;
  19. private $m_aId2Row;
  20. private $m_iCurrRow;
  21. public function __construct(DBObjectSearch $oFilter, $aOrderBy = array(), $aArgs = array())
  22. {
  23. $this->m_oFilter = $oFilter;
  24. $this->m_aOrderBy = $aOrderBy;
  25. $this->m_aArgs = $aArgs;
  26. $this->m_bLoaded = false;
  27. $this->m_aData = array();
  28. $this->m_aId2Row = array();
  29. $this->m_iCurrRow = 0;
  30. }
  31. public function __destruct()
  32. {
  33. }
  34. public function __toString()
  35. {
  36. $sRet = '';
  37. $this->Rewind();
  38. $sRet .= "Set (".$this->m_oFilter->ToSibuSQL().")<br/>\n";
  39. $sRet .= "Query: <pre style=\"font-size: smaller; display:inline;\">".MetaModel::MakeSelectQuery($this->m_oFilter, array()).")</pre>\n";
  40. $sRet .= $this->Count()." records<br/>\n";
  41. if ($this->Count() > 0)
  42. {
  43. $sRet .= "<ul class=\"treeview\">\n";
  44. while ($oObj = $this->Fetch())
  45. {
  46. $sRet .= "<li>".$oObj->__toString()."</li>\n";
  47. }
  48. $sRet .= "</ul>\n";
  49. }
  50. return $sRet;
  51. }
  52. static public function FromObject($oObject)
  53. {
  54. $oRetSet = self::FromScratch(get_class($oObject));
  55. $oRetSet->AddObject($oObject);
  56. return $oRetSet;
  57. }
  58. static public function FromScratch($sClass)
  59. {
  60. $oFilter = new CMDBSearchFilter($sClass);
  61. $oRetSet = new self($oFilter);
  62. $oRetSet->m_bLoaded = true; // no DB load
  63. return $oRetSet;
  64. }
  65. static public function FromArray($sClass, $aObjects)
  66. {
  67. $oFilter = new CMDBSearchFilter($sClass);
  68. $oRetSet = new self($oFilter);
  69. $oRetSet->m_bLoaded = true; // no DB load
  70. $oRetSet->AddObjectArray($aObjects);
  71. return $oRetSet;
  72. }
  73. static public function FromLinkSet($oObject, $sLinkSetAttCode, $sExtKeyToRemote)
  74. {
  75. $oLinkAttCode = MetaModel::GetAttributeDef(get_class($oObject), $sLinkSetAttCode);
  76. $oExtKeyAttDef = MetaModel::GetAttributeDef($oLinkAttCode->GetLinkedClass(), $sExtKeyToRemote);
  77. $sTargetClass = $oExtKeyAttDef->GetTargetClass();
  78. $oLinkSet = $oObject->Get($sLinkSetAttCode);
  79. $aTargets = array();
  80. while ($oLink = $oLinkSet->Fetch())
  81. {
  82. $aTargets[] = MetaModel::GetObject($sTargetClass, $oLink->Get($sExtKeyToRemote));
  83. }
  84. return self::FromArray($sTargetClass, $aTargets);
  85. }
  86. public function ToArray($bWithId = true)
  87. {
  88. $aRet = array();
  89. $this->Rewind();
  90. while ($oObject = $this->Fetch())
  91. {
  92. if ($bWithId)
  93. {
  94. $aRet[$oObject->GetKey()] = $oObject;
  95. }
  96. else
  97. {
  98. $aRet[] = $oObject;
  99. }
  100. }
  101. return $aRet;
  102. }
  103. public function GetColumnAsArray($sAttCode, $bWithId = true)
  104. {
  105. $aRet = array();
  106. $this->Rewind();
  107. while ($oObject = $this->Fetch())
  108. {
  109. if ($bWithId)
  110. {
  111. $aRet[$oObject->GetKey()] = $oObject->Get($sAttCode);
  112. }
  113. else
  114. {
  115. $aRet[] = $oObject->Get($sAttCode);
  116. }
  117. }
  118. return $aRet;
  119. }
  120. public function GetFilter()
  121. {
  122. return $this->m_oFilter;
  123. }
  124. public function GetClass()
  125. {
  126. return $this->m_oFilter->GetClass();
  127. }
  128. public function GetRootClass()
  129. {
  130. return MetaModel::GetRootClass($this->GetClass());
  131. }
  132. public function Load()
  133. {
  134. if ($this->m_bLoaded) return;
  135. $sSQL = MetaModel::MakeSelectQuery($this->m_oFilter, $this->m_aOrderBy, $this->m_aArgs);
  136. $resQuery = CMDBSource::Query($sSQL);
  137. if (!$resQuery) return;
  138. while ($aRow = CMDBSource::FetchArray($resQuery))
  139. {
  140. $sClass = $this->m_oFilter->GetClass();
  141. $oObject = MetaModel::GetObjectByRow($sClass, $aRow);
  142. $this->AddObject($oObject);
  143. }
  144. CMDBSource::FreeResult($resQuery);
  145. $this->m_bLoaded = true;
  146. }
  147. public function Count()
  148. {
  149. if (!$this->m_bLoaded) $this->Load();
  150. return count($this->m_aData);
  151. }
  152. public function Fetch()
  153. {
  154. if (!$this->m_bLoaded) $this->Load();
  155. if ($this->m_iCurrRow >= count($this->m_aData))
  156. {
  157. return null;
  158. }
  159. $oRetObj = $this->m_aData[$this->m_iCurrRow];
  160. $this->m_iCurrRow++;
  161. return $oRetObj;
  162. }
  163. public function Rewind()
  164. {
  165. $this->Seek(0);
  166. }
  167. public function Seek($iRow)
  168. {
  169. if (!$this->m_bLoaded) $this->Load();
  170. $this->m_iCurrRow = min($iRow, count($this->m_aData));
  171. return $this->m_iCurrRow;
  172. }
  173. public function AddObject($oObject)
  174. {
  175. // ?usefull? if ($oObject->GetClass() != $this->GetClass()) return;
  176. // it is mandatory to avoid duplicates
  177. if (array_key_exists($oObject->GetKey(), $this->m_aId2Row)) return;
  178. // Do not load here, because the load uses that method too
  179. $iNextPos = count($this->m_aData);
  180. $this->m_aData[$iNextPos] = $oObject;
  181. $this->m_aId2Row[$oObject->GetKey()] = $iNextPos;
  182. }
  183. public function AddObjectArray($aObjects)
  184. {
  185. // #@# todo - add a check on the object class ?
  186. foreach ($aObjects as $oObj)
  187. {
  188. $this->AddObject($oObj);
  189. }
  190. }
  191. public function Merge($oObjectSet)
  192. {
  193. if ($this->GetRootClass() != $oObjectSet->GetRootClass())
  194. {
  195. throw new CoreException("Could not merge two objects sets if they don't have the same root class");
  196. }
  197. if (!$this->m_bLoaded) $this->Load();
  198. $oObjectSet->Seek(0);
  199. while ($oObject = $oObjectSet->Fetch())
  200. {
  201. $this->AddObject($oObject);
  202. }
  203. }
  204. public function CreateIntersect($oObjectSet)
  205. {
  206. if ($this->GetRootClass() != $oObjectSet->GetRootClass())
  207. {
  208. throw new CoreException("Could not 'intersect' two objects sets if they don't have the same root class");
  209. }
  210. if (!$this->m_bLoaded) $this->Load();
  211. $oNewSet = DBObjectSet::FromScratch($this->GetClass());
  212. $oObjectSet->Seek(0);
  213. while ($oObject = $oObjectSet->Fetch())
  214. {
  215. if (array_key_exists($oObject->GetKey(), $this->m_aId2Row))
  216. {
  217. $oNewSet->AddObject($oObject);
  218. }
  219. }
  220. return $oNewSet;
  221. }
  222. public function CreateDelta($oObjectSet)
  223. {
  224. if ($this->GetRootClass() != $oObjectSet->GetRootClass())
  225. {
  226. throw new CoreException("Could not 'delta' two objects sets if they don't have the same root class");
  227. }
  228. if (!$this->m_bLoaded) $this->Load();
  229. $oNewSet = DBObjectSet::FromScratch($this->GetClass());
  230. $oObjectSet->Seek(0);
  231. while ($oObject = $oObjectSet->Fetch())
  232. {
  233. if (!array_key_exists($oObject->GetKey(), $this->m_aId2Row))
  234. {
  235. $oNewSet->AddObject($oObject);
  236. }
  237. }
  238. return $oNewSet;
  239. }
  240. public function GetRelatedObjects($sRelCode, $iMaxDepth = 99)
  241. {
  242. $aRelatedObjs = array();
  243. $aVisited = array(); // optimization for consecutive calls of MetaModel::GetRelatedObjects
  244. $this->Seek(0);
  245. while ($oObject = $this->Fetch())
  246. {
  247. // #@# todo - actually merge !
  248. $aRelatedObjs = array_merge_recursive($aRelatedObjs, $oObject->GetRelatedObjects($sRelCode, $iMaxDepth, $aVisited));
  249. }
  250. return $aRelatedObjs;
  251. }
  252. }
  253. ?>