dbobjectset.class.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  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(); // array of (row => array of (classalias) => object)
  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 GetSelectedClasses()
  129. {
  130. return $this->m_oFilter->GetSelectedClasses();
  131. }
  132. public function GetRootClass()
  133. {
  134. return MetaModel::GetRootClass($this->GetClass());
  135. }
  136. public function Load()
  137. {
  138. if ($this->m_bLoaded) return;
  139. $sSQL = MetaModel::MakeSelectQuery($this->m_oFilter, $this->m_aOrderBy, $this->m_aArgs);
  140. $resQuery = CMDBSource::Query($sSQL);
  141. if (!$resQuery) return;
  142. $sClass = $this->m_oFilter->GetClass();
  143. while ($aRow = CMDBSource::FetchArray($resQuery))
  144. {
  145. $aObjects = array();
  146. foreach ($this->m_oFilter->GetSelectedClasses() as $sClassAlias => $sClass)
  147. {
  148. $oObject = MetaModel::GetObjectByRow($sClass, $aRow, $sClassAlias);
  149. $aObjects[$sClassAlias] = $oObject;
  150. }
  151. $this->AddObjectExtended($aObjects);
  152. }
  153. CMDBSource::FreeResult($resQuery);
  154. $this->m_bLoaded = true;
  155. }
  156. public function Count()
  157. {
  158. if (!$this->m_bLoaded) $this->Load();
  159. return count($this->m_aData);
  160. }
  161. public function Fetch($sClassAlias = '')
  162. {
  163. if (!$this->m_bLoaded) $this->Load();
  164. if ($this->m_iCurrRow >= count($this->m_aData))
  165. {
  166. return null;
  167. }
  168. if (strlen($sClassAlias) == 0)
  169. {
  170. $sClassAlias = $this->m_oFilter->GetClassAlias();
  171. }
  172. $oRetObj = $this->m_aData[$this->m_iCurrRow][$sClassAlias];
  173. $this->m_iCurrRow++;
  174. return $oRetObj;
  175. }
  176. // Return the whole line if several classes have been specified in the query
  177. //
  178. public function FetchAssoc()
  179. {
  180. if (!$this->m_bLoaded) $this->Load();
  181. if ($this->m_iCurrRow >= count($this->m_aData))
  182. {
  183. return null;
  184. }
  185. $aRetObjects = $this->m_aData[$this->m_iCurrRow];
  186. $this->m_iCurrRow++;
  187. return $aRetObjects;
  188. }
  189. public function Rewind()
  190. {
  191. $this->Seek(0);
  192. }
  193. public function Seek($iRow)
  194. {
  195. if (!$this->m_bLoaded) $this->Load();
  196. $this->m_iCurrRow = min($iRow, count($this->m_aData));
  197. return $this->m_iCurrRow;
  198. }
  199. public function AddObject($oObject, $sClassAlias = '')
  200. {
  201. $sObjClass = get_class($oObject);
  202. if (strlen($sClassAlias) == 0)
  203. {
  204. $sClassAlias = $sObjClass;
  205. }
  206. $iNextPos = count($this->m_aData);
  207. $this->m_aData[$iNextPos][$sClassAlias] = $oObject;
  208. $this->m_aId2Row[$sObjClass][$oObject->GetKey()] = $iNextPos;
  209. }
  210. protected function AddObjectExtended($aObjectArray)
  211. {
  212. $iNextPos = count($this->m_aData);
  213. foreach ($aObjectArray as $sClassAlias => $oObject)
  214. {
  215. $this->m_aData[$iNextPos][$sClassAlias] = $oObject;
  216. $this->m_aId2Row[get_class($oObject)][$oObject->GetKey()] = $iNextPos;
  217. }
  218. }
  219. public function AddObjectArray($aObjects, $sClassAlias = '')
  220. {
  221. // #@# todo - add a check on the object class ?
  222. foreach ($aObjects as $oObj)
  223. {
  224. $this->AddObject($oObj, $sClassAlias);
  225. }
  226. }
  227. public function Merge($oObjectSet)
  228. {
  229. if ($this->GetRootClass() != $oObjectSet->GetRootClass())
  230. {
  231. throw new CoreException("Could not merge two objects sets if they don't have the same root class");
  232. }
  233. if (!$this->m_bLoaded) $this->Load();
  234. $oObjectSet->Seek(0);
  235. while ($oObject = $oObjectSet->Fetch())
  236. {
  237. $this->AddObject($oObject);
  238. }
  239. }
  240. public function CreateIntersect($oObjectSet)
  241. {
  242. if ($this->GetRootClass() != $oObjectSet->GetRootClass())
  243. {
  244. throw new CoreException("Could not 'intersect' two objects sets if they don't have the same root class");
  245. }
  246. if (!$this->m_bLoaded) $this->Load();
  247. $oNewSet = DBObjectSet::FromScratch($this->GetClass());
  248. $oObjectSet->Seek(0);
  249. while ($oObject = $oObjectSet->Fetch())
  250. {
  251. if (array_key_exists($oObject->GetKey(), $this->m_aId2Row))
  252. {
  253. $oNewSet->AddObject($oObject);
  254. }
  255. }
  256. return $oNewSet;
  257. }
  258. public function CreateDelta($oObjectSet)
  259. {
  260. if ($this->GetRootClass() != $oObjectSet->GetRootClass())
  261. {
  262. throw new CoreException("Could not 'delta' two objects sets if they don't have the same root class");
  263. }
  264. if (!$this->m_bLoaded) $this->Load();
  265. $oNewSet = DBObjectSet::FromScratch($this->GetClass());
  266. $oObjectSet->Seek(0);
  267. while ($oObject = $oObjectSet->Fetch())
  268. {
  269. if (!array_key_exists($oObject->GetKey(), $this->m_aId2Row))
  270. {
  271. $oNewSet->AddObject($oObject);
  272. }
  273. }
  274. return $oNewSet;
  275. }
  276. public function GetRelatedObjects($sRelCode, $iMaxDepth = 99)
  277. {
  278. $aRelatedObjs = array();
  279. $aVisited = array(); // optimization for consecutive calls of MetaModel::GetRelatedObjects
  280. $this->Seek(0);
  281. while ($oObject = $this->Fetch())
  282. {
  283. // #@# todo - actually merge !
  284. $aRelatedObjs = array_merge_recursive($aRelatedObjs, $oObject->GetRelatedObjects($sRelCode, $iMaxDepth, $aVisited));
  285. }
  286. return $aRelatedObjs;
  287. }
  288. }
  289. ?>