123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188 |
- <?php
- class PropelArrayCollection extends PropelCollection
- {
- protected $workerObject;
-
- public function save($con = null)
- {
- if (null === $con) {
- $con = $this->getConnection(Propel::CONNECTION_WRITE);
- }
- $con->beginTransaction();
- try {
- $obj = $this->getWorkerObject();
- foreach ($this as $element) {
- $obj->clear();
- $obj->fromArray($element);
- $obj->setNew($obj->isPrimaryKeyNull());
- $obj->save($con);
- }
- $con->commit();
- } catch (PropelException $e) {
- $con->rollback();
- }
- }
-
-
- public function delete($con = null)
- {
- if (null === $con) {
- $con = $this->getConnection(Propel::CONNECTION_WRITE);
- }
- $con->beginTransaction();
- try {
- foreach ($this as $element) {
- $obj = $this->getWorkerObject();
- $obj->setDeleted(false);
- $obj->fromArray($element);
- $obj->delete($con);
- }
- $con->commit();
- } catch (PropelException $e) {
- $con->rollback();
- throw $e;
- }
- }
-
- public function getPrimaryKeys($usePrefix = true)
- {
- $callable = array($this->getPeerClass(), 'getPrimaryKeyFromRow');
- $ret = array();
- foreach ($this as $key => $element) {
- $key = $usePrefix ? ($this->getModel() . '_' . $key) : $key;
- $ret[$key]= call_user_func($callable, array_values($element));
- }
-
- return $ret;
- }
-
- public function fromArray($arr)
- {
- $obj = $this->getWorkerObject();
- foreach ($arr as $element) {
- $obj->clear();
- $obj->fromArray($element);
- $this->append($obj->toArray());
- }
- }
-
-
- public function toArray($keyColumn = null, $usePrefix = false)
- {
- $ret = array();
- foreach ($this as $key => $element) {
- $key = null === $keyColumn ? $key : $element[$keyColumn];
- $key = $usePrefix ? ($this->getModel() . '_' . $key) : $key;
- $ret[$key] = $element;
- }
- return $ret;
- }
-
-
- public function getArrayCopy($keyColumn = null, $usePrefix = false)
- {
- if (null === $keyColumn && false === $usePrefix) {
- return parent::getArrayCopy();
- } else {
- return $this->toArray($keyColumn, $usePrefix);
- }
- }
-
-
- public function toKeyValue($keyColumn, $valueColumn)
- {
- $ret = array();
- foreach ($this as $obj) {
- $ret[$obj[$keyColumn]] = $obj[$valueColumn];
- }
-
- return $ret;
- }
- protected function getWorkerObject()
- {
- if (null === $this->workerObject) {
- if ($this->model == '') {
- throw new PropelException('You must set the collection model before interacting with it');
- }
- $class = $this->getModel();
- $this->workerObject = new $class();
- }
-
- return $this->workerObject;
- }
- }
- ?>
|