PropelArrayCollection.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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 stored as arrays
  11. *
  12. * @author Francois Zaninotto
  13. * @package propel.runtime.collection
  14. */
  15. class PropelArrayCollection extends PropelCollection
  16. {
  17. protected $workerObject;
  18. /**
  19. * Save all the elements in the collection
  20. */
  21. public function save($con = null)
  22. {
  23. if (null === $con) {
  24. $con = $this->getConnection(Propel::CONNECTION_WRITE);
  25. }
  26. $con->beginTransaction();
  27. try {
  28. $obj = $this->getWorkerObject();
  29. foreach ($this as $element) {
  30. $obj->clear();
  31. $obj->fromArray($element);
  32. $obj->setNew($obj->isPrimaryKeyNull());
  33. $obj->save($con);
  34. }
  35. $con->commit();
  36. } catch (PropelException $e) {
  37. $con->rollback();
  38. }
  39. }
  40. /**
  41. * Delete all the elements in the collection
  42. */
  43. public function delete($con = null)
  44. {
  45. if (null === $con) {
  46. $con = $this->getConnection(Propel::CONNECTION_WRITE);
  47. }
  48. $con->beginTransaction();
  49. try {
  50. foreach ($this as $element) {
  51. $obj = $this->getWorkerObject();
  52. $obj->setDeleted(false);
  53. $obj->fromArray($element);
  54. $obj->delete($con);
  55. }
  56. $con->commit();
  57. } catch (PropelException $e) {
  58. $con->rollback();
  59. throw $e;
  60. }
  61. }
  62. /**
  63. * Get an array of the primary keys of all the objects in the collection
  64. *
  65. * @return array The list of the primary keys of the collection
  66. */
  67. public function getPrimaryKeys($usePrefix = true)
  68. {
  69. $callable = array($this->getPeerClass(), 'getPrimaryKeyFromRow');
  70. $ret = array();
  71. foreach ($this as $key => $element) {
  72. $key = $usePrefix ? ($this->getModel() . '_' . $key) : $key;
  73. $ret[$key]= call_user_func($callable, array_values($element));
  74. }
  75. return $ret;
  76. }
  77. /**
  78. * Populates the collection from an array
  79. * Uses the object model to force the column types
  80. * Does not empty the collection before adding the data from the array
  81. *
  82. * @param array $arr
  83. */
  84. public function fromArray($arr)
  85. {
  86. $obj = $this->getWorkerObject();
  87. foreach ($arr as $element) {
  88. $obj->clear();
  89. $obj->fromArray($element);
  90. $this->append($obj->toArray());
  91. }
  92. }
  93. /**
  94. * Get an array representation of the collection
  95. * This is not an alias for getData(), since it returns a copy of the data
  96. *
  97. * @param string $keyColumn If null, the returned array uses an incremental index.
  98. * Otherwise, the array is indexed using the specified column
  99. * @param boolean $usePrefix If true, the returned array prefixes keys
  100. * with the model class name ('Article_0', 'Article_1', etc).
  101. *
  102. * <code>
  103. * $bookCollection->toArray();
  104. * array(
  105. * 0 => array('Id' => 123, 'Title' => 'War And Peace'),
  106. * 1 => array('Id' => 456, 'Title' => 'Don Juan'),
  107. * )
  108. * $bookCollection->toArray('Id');
  109. * array(
  110. * 123 => array('Id' => 123, 'Title' => 'War And Peace'),
  111. * 456 => array('Id' => 456, 'Title' => 'Don Juan'),
  112. * )
  113. * $bookCollection->toArray(null, true);
  114. * array(
  115. * 'Book_0' => array('Id' => 123, 'Title' => 'War And Peace'),
  116. * 'Book_1' => array('Id' => 456, 'Title' => 'Don Juan'),
  117. * )
  118. * </code>
  119. * @return array
  120. */
  121. public function toArray($keyColumn = null, $usePrefix = false)
  122. {
  123. $ret = array();
  124. foreach ($this as $key => $element) {
  125. $key = null === $keyColumn ? $key : $element[$keyColumn];
  126. $key = $usePrefix ? ($this->getModel() . '_' . $key) : $key;
  127. $ret[$key] = $element;
  128. }
  129. return $ret;
  130. }
  131. /**
  132. * Synonym for toArray(), to provide a similar interface to PopelObjectCollection
  133. */
  134. public function getArrayCopy($keyColumn = null, $usePrefix = false)
  135. {
  136. if (null === $keyColumn && false === $usePrefix) {
  137. return parent::getArrayCopy();
  138. } else {
  139. return $this->toArray($keyColumn, $usePrefix);
  140. }
  141. }
  142. /**
  143. * Get an associative array representation of the collection
  144. * The first parameter specifies the column to be used for the key,
  145. * And the seconf for the value.
  146. * <code>
  147. * $res = $coll->toKeyValue('Id', 'Name');
  148. * </code>
  149. *
  150. * @return array
  151. */
  152. public function toKeyValue($keyColumn, $valueColumn)
  153. {
  154. $ret = array();
  155. foreach ($this as $obj) {
  156. $ret[$obj[$keyColumn]] = $obj[$valueColumn];
  157. }
  158. return $ret;
  159. }
  160. protected function getWorkerObject()
  161. {
  162. if (null === $this->workerObject) {
  163. if ($this->model == '') {
  164. throw new PropelException('You must set the collection model before interacting with it');
  165. }
  166. $class = $this->getModel();
  167. $this->workerObject = new $class();
  168. }
  169. return $this->workerObject;
  170. }
  171. }
  172. ?>