PageRenderTime 45ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/runtime/lib/collection/PropelArrayCollection.php

http://github.com/propelorm/Propel
PHP | 214 lines | 105 code | 14 blank | 95 comment | 10 complexity | 367a4f69f1866d9a2517ddb6176cea5a MD5 | raw file
  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. * @param PropelPDO $con
  22. *
  23. * @throws PropelException
  24. */
  25. public function save($con = null)
  26. {
  27. if (!method_exists($this->getModel(), 'save')) {
  28. throw new PropelException('Cannot save objects on a read-only model');
  29. }
  30. if (null === $con) {
  31. $con = $this->getConnection(Propel::CONNECTION_WRITE);
  32. }
  33. $con->beginTransaction();
  34. try {
  35. $obj = $this->getWorkerObject();
  36. foreach ($this as $element) {
  37. $obj->clear();
  38. $obj->fromArray($element);
  39. $obj->setNew($obj->isPrimaryKeyNull());
  40. $obj->save($con);
  41. }
  42. $con->commit();
  43. } catch (Exception $e) {
  44. $con->rollback();
  45. }
  46. }
  47. /**
  48. * Delete all the elements in the collection
  49. *
  50. * @param PropelPDO $con
  51. *
  52. * @throws PropelException
  53. */
  54. public function delete($con = null)
  55. {
  56. if (!method_exists($this->getModel(), 'delete')) {
  57. throw new PropelException('Cannot delete objects on a read-only model');
  58. }
  59. if (null === $con) {
  60. $con = $this->getConnection(Propel::CONNECTION_WRITE);
  61. }
  62. $con->beginTransaction();
  63. try {
  64. foreach ($this as $element) {
  65. $obj = $this->getWorkerObject();
  66. $obj->setDeleted(false);
  67. $obj->fromArray($element);
  68. $obj->delete($con);
  69. }
  70. $con->commit();
  71. } catch (Exception $e) {
  72. $con->rollback();
  73. throw $e;
  74. }
  75. }
  76. /**
  77. * Get an array of the primary keys of all the objects in the collection
  78. *
  79. * @param boolean $usePrefix
  80. *
  81. * @return array The list of the primary keys of the collection
  82. */
  83. public function getPrimaryKeys($usePrefix = true)
  84. {
  85. $callable = array($this->getPeerClass(), 'getPrimaryKeyFromRow');
  86. $ret = array();
  87. foreach ($this as $key => $element) {
  88. $key = $usePrefix ? ($this->getModel() . '_' . $key) : $key;
  89. $ret[$key] = call_user_func($callable, array_values($element));
  90. }
  91. return $ret;
  92. }
  93. /**
  94. * Populates the collection from an array
  95. * Uses the object model to force the column types
  96. * Does not empty the collection before adding the data from the array
  97. *
  98. * @param array $arr
  99. */
  100. public function fromArray($arr)
  101. {
  102. $obj = $this->getWorkerObject();
  103. foreach ($arr as $element) {
  104. $obj->clear();
  105. $obj->fromArray($element);
  106. $this->append($obj->toArray());
  107. }
  108. }
  109. /**
  110. * Get an array representation of the collection
  111. * This is not an alias for getData(), since it returns a copy of the data
  112. *
  113. * @param string $keyColumn If null, the returned array uses an incremental index.
  114. * Otherwise, the array is indexed using the specified column
  115. * @param boolean $usePrefix If true, the returned array prefixes keys
  116. * with the model class name ('Article_0', 'Article_1', etc).
  117. *
  118. * <code>
  119. * $bookCollection->toArray();
  120. * array(
  121. * 0 => array('Id' => 123, 'Title' => 'War And Peace'),
  122. * 1 => array('Id' => 456, 'Title' => 'Don Juan'),
  123. * )
  124. * $bookCollection->toArray('Id');
  125. * array(
  126. * 123 => array('Id' => 123, 'Title' => 'War And Peace'),
  127. * 456 => array('Id' => 456, 'Title' => 'Don Juan'),
  128. * )
  129. * $bookCollection->toArray(null, true);
  130. * array(
  131. * 'Book_0' => array('Id' => 123, 'Title' => 'War And Peace'),
  132. * 'Book_1' => array('Id' => 456, 'Title' => 'Don Juan'),
  133. * )
  134. * </code>
  135. *
  136. * @return array
  137. */
  138. public function toArray($keyColumn = null, $usePrefix = false, $keyType = BasePeer::TYPE_PHPNAME, $includeLazyLoadColumns = true, $alreadyDumpedObjects = array())
  139. {
  140. $ret = array();
  141. foreach ($this as $key => $element) {
  142. $key = null === $keyColumn ? $key : $element[$keyColumn];
  143. $key = $usePrefix ? ($this->getModel() . '_' . $key) : $key;
  144. $ret[$key] = $element;
  145. }
  146. return $ret;
  147. }
  148. /**
  149. * Synonym for toArray(), to provide a similar interface to PopelObjectCollection
  150. *
  151. * @param string $keyColumn
  152. * @param boolean $usePrefix
  153. *
  154. * @return array
  155. */
  156. public function getArrayCopy($keyColumn = null, $usePrefix = false)
  157. {
  158. if (null === $keyColumn && false === $usePrefix) {
  159. return parent::getArrayCopy();
  160. } else {
  161. return $this->toArray($keyColumn, $usePrefix);
  162. }
  163. }
  164. /**
  165. * Get an associative array representation of the collection
  166. * The first parameter specifies the column to be used for the key,
  167. * And the seconf for the value.
  168. * <code>
  169. * $res = $coll->toKeyValue('Id', 'Name');
  170. * </code>
  171. *
  172. * @param string $keyColumn
  173. * @param string $valueColumn
  174. *
  175. * @return array
  176. */
  177. public function toKeyValue($keyColumn, $valueColumn)
  178. {
  179. $ret = array();
  180. foreach ($this as $obj) {
  181. $ret[$obj[$keyColumn]] = $obj[$valueColumn];
  182. }
  183. return $ret;
  184. }
  185. /**
  186. * @throws PropelException
  187. * @return BaseObject
  188. */
  189. protected function getWorkerObject()
  190. {
  191. if (null === $this->workerObject) {
  192. if ($this->model == '') {
  193. throw new PropelException('You must set the collection model before interacting with it');
  194. }
  195. $class = $this->getModel();
  196. $this->workerObject = new $class();
  197. }
  198. return $this->workerObject;
  199. }
  200. }