PropelObjectCollection.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. <?php
  2. /**
  3. * This file is part of the Propel package.
  4. * For the full copyright and license information, please view the LICENSE
  5. * file that was distributed with this source code.
  6. *
  7. * @license MIT License
  8. */
  9. /**
  10. * Class for iterating over a list of Propel objects
  11. *
  12. * @author Francois Zaninotto
  13. * @package propel.runtime.collection
  14. */
  15. class PropelObjectCollection extends PropelCollection
  16. {
  17. /**
  18. * Save all the elements in the collection
  19. */
  20. public function save($con = null)
  21. {
  22. if (null === $con) {
  23. $con = $this->getConnection(Propel::CONNECTION_WRITE);
  24. }
  25. $con->beginTransaction();
  26. try {
  27. foreach ($this as $element) {
  28. $element->save($con);
  29. }
  30. $con->commit();
  31. } catch (PropelException $e) {
  32. $con->rollback();
  33. throw $e;
  34. }
  35. }
  36. /**
  37. * Delete all the elements in the collection
  38. */
  39. public function delete($con = null)
  40. {
  41. if (null === $con) {
  42. $con = $this->getConnection(Propel::CONNECTION_WRITE);
  43. }
  44. $con->beginTransaction();
  45. try {
  46. foreach ($this as $element) {
  47. $element->delete($con);
  48. }
  49. $con->commit();
  50. } catch (PropelException $e) {
  51. $con->rollback();
  52. throw $e;
  53. }
  54. }
  55. /**
  56. * Get an array of the primary keys of all the objects in the collection
  57. *
  58. * @return array The list of the primary keys of the collection
  59. */
  60. public function getPrimaryKeys($usePrefix = true)
  61. {
  62. $ret = array();
  63. foreach ($this as $key => $obj) {
  64. $key = $usePrefix ? ($this->getModel() . '_' . $key) : $key;
  65. $ret[$key]= $obj->getPrimaryKey();
  66. }
  67. return $ret;
  68. }
  69. /**
  70. * Populates the collection from an array
  71. * Each object is populated from an array and the result is stored
  72. * Does not empty the collection before adding the data from the array
  73. *
  74. * @param array $arr
  75. */
  76. public function fromArray($arr)
  77. {
  78. $class = $this->getModel();
  79. foreach ($arr as $element) {
  80. $obj = new $class();
  81. $obj->fromArray($element);
  82. $this->append($obj);
  83. }
  84. }
  85. /**
  86. * Get an array representation of the collection
  87. * Each object is turned into an array and the result is returned
  88. *
  89. * @param string $keyColumn If null, the returned array uses an incremental index.
  90. * Otherwise, the array is indexed using the specified column
  91. * @param boolean $usePrefix If true, the returned array prefixes keys
  92. * with the model class name ('Article_0', 'Article_1', etc).
  93. *
  94. * <code>
  95. * $bookCollection->toArray();
  96. * array(
  97. * 0 => array('Id' => 123, 'Title' => 'War And Peace'),
  98. * 1 => array('Id' => 456, 'Title' => 'Don Juan'),
  99. * )
  100. * $bookCollection->toArray('Id');
  101. * array(
  102. * 123 => array('Id' => 123, 'Title' => 'War And Peace'),
  103. * 456 => array('Id' => 456, 'Title' => 'Don Juan'),
  104. * )
  105. * $bookCollection->toArray(null, true);
  106. * array(
  107. * 'Book_0' => array('Id' => 123, 'Title' => 'War And Peace'),
  108. * 'Book_1' => array('Id' => 456, 'Title' => 'Don Juan'),
  109. * )
  110. * </code>
  111. * @return array
  112. */
  113. public function toArray($keyColumn = null, $usePrefix = false)
  114. {
  115. $ret = array();
  116. $keyGetterMethod = 'get' . $keyColumn;
  117. foreach ($this as $key => $obj) {
  118. $key = null === $keyColumn ? $key : $obj->$keyGetterMethod();
  119. $key = $usePrefix ? ($this->getModel() . '_' . $key) : $key;
  120. $ret[$key] = $obj->toArray();
  121. }
  122. return $ret;
  123. }
  124. /**
  125. * Get an array representation of the collection
  126. *
  127. * @param string $keyColumn If null, the returned array uses an incremental index.
  128. * Otherwise, the array is indexed using the specified column
  129. * @param boolean $usePrefix If true, the returned array prefixes keys
  130. * with the model class name ('Article_0', 'Article_1', etc).
  131. *
  132. * <code>
  133. * $bookCollection->getArrayCopy();
  134. * array(
  135. * 0 => $book0,
  136. * 1 => $book1,
  137. * )
  138. * $bookCollection->getArrayCopy('Id');
  139. * array(
  140. * 123 => $book0,
  141. * 456 => $book1,
  142. * )
  143. * $bookCollection->getArrayCopy(null, true);
  144. * array(
  145. * 'Book_0' => $book0,
  146. * 'Book_1' => $book1,
  147. * )
  148. * </code>
  149. * @return array
  150. */
  151. public function getArrayCopy($keyColumn = null, $usePrefix = false)
  152. {
  153. if (null === $keyColumn && false === $usePrefix) {
  154. return parent::getArrayCopy();
  155. }
  156. $ret = array();
  157. $keyGetterMethod = 'get' . $keyColumn;
  158. foreach ($this as $key => $obj) {
  159. $key = null === $keyColumn ? $key : $obj->$keyGetterMethod();
  160. $key = $usePrefix ? ($this->getModel() . '_' . $key) : $key;
  161. $ret[$key] = $obj;
  162. }
  163. return $ret;
  164. }
  165. /**
  166. * Get an associative array representation of the collection
  167. * The first parameter specifies the column to be used for the key,
  168. * And the seconf for the value.
  169. * <code>
  170. * $res = $coll->toKeyValue('Id', 'Name');
  171. * </code>
  172. *
  173. * @return array
  174. */
  175. public function toKeyValue($keyColumn = 'PrimaryKey', $valueColumn = null)
  176. {
  177. $ret = array();
  178. $keyGetterMethod = 'get' . $keyColumn;
  179. $valueGetterMethod = (null === $valueColumn) ? '__toString' : ('get' . $valueColumn);
  180. foreach ($this as $obj) {
  181. $ret[$obj->$keyGetterMethod()] = $obj->$valueGetterMethod();
  182. }
  183. return $ret;
  184. }
  185. /**
  186. * Makes an additional query to populate the objects related to the collection objects
  187. * by a certain relation
  188. *
  189. * @param string $relation Relation name (e.g. 'Book')
  190. * @param Criteria $criteria Optional Criteria object to filter the related object collection
  191. * @param PropelPDO $con Optional connection object
  192. *
  193. * @return PropelObjectCollection the list of related objects
  194. */
  195. public function populateRelation($relation, $criteria = null, $con = null)
  196. {
  197. if (!Propel::isInstancePoolingEnabled()) {
  198. throw new PropelException('populateRelation() needs instance pooling to be enabled prior to populating the collection');
  199. }
  200. $relationMap = $this->getFormatter()->getTableMap()->getRelation($relation);
  201. $symRelationMap = $relationMap->getSymmetricalRelation();
  202. // query the db for the related objects
  203. $useMethod = 'use' . $symRelationMap->getName() . 'Query';
  204. $query = PropelQuery::from($relationMap->getRightTable()->getPhpName());
  205. if (null !== $criteria) {
  206. $query->mergeWith($criteria);
  207. }
  208. $relatedObjects = $query
  209. ->$useMethod()
  210. ->filterByPrimaryKeys($this->getPrimaryKeys())
  211. ->endUse()
  212. ->find($con);
  213. // associate the related objects to the main objects
  214. if ($relationMap->getType() == RelationMap::ONE_TO_MANY) {
  215. $getMethod = 'get' . $symRelationMap->getName();
  216. $addMethod = 'add' . $relationMap->getName();
  217. foreach ($relatedObjects as $object) {
  218. $mainObj = $object->$getMethod(); // instance pool is used here to avoid a query
  219. $mainObj->$addMethod($object);
  220. }
  221. } elseif ($relationMap->getType() == RelationMap::MANY_TO_ONE) {
  222. // nothing to do; the instance pool will catch all calls to getRelatedObject()
  223. // and return the object in memory
  224. } else {
  225. throw new PropelException('populateRelation() does not support this relation type');
  226. }
  227. return $relatedObjects;
  228. }
  229. }
  230. ?>