PageRenderTime 21ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/src/Propel/Runtime/Collection/ArrayCollection.php

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