Collection.php 20 KB
Newer Older
lsmith's avatar
lsmith committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
<?php
/*
 *  $Id$
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * This software consists of voluntary contributions made by many individuals
 * and is licensed under the LGPL. For more information, see
 * <http://www.phpdoctrine.com>.
 */
zYne's avatar
zYne committed
21
Doctrine::autoload('Doctrine_Access');
lsmith's avatar
lsmith committed
22 23 24 25 26
/**
 * Doctrine_Collection
 * Collection of Doctrine_Record objects.
 *
 * @package     Doctrine
27
 * @subpackage  Collection
lsmith's avatar
lsmith committed
28 29 30 31 32 33 34 35 36
 * @license     http://www.opensource.org/licenses/lgpl-license.php LGPL
 * @link        www.phpdoctrine.com
 * @since       1.0
 * @version     $Revision$
 * @author      Konsta Vesterinen <kvesteri@cc.hut.fi>
 */
class Doctrine_Collection extends Doctrine_Access implements Countable, IteratorAggregate, Serializable
{
    /**
zYne's avatar
zYne committed
37
     * @var array $data                     an array containing the records of this collection
lsmith's avatar
lsmith committed
38 39 40 41 42
     */
    protected $data = array();
    /**
     * @var Doctrine_Table $table           each collection has only records of specified table
     */
zYne's avatar
zYne committed
43 44 45 46 47
    protected $_table;
    /**
     * @var array $_snapshot                a snapshot of the fetched data
     */
    protected $_snapshot = array();
lsmith's avatar
lsmith committed
48 49 50 51 52
    /**
     * @var Doctrine_Record $reference      collection can belong to a record
     */
    protected $reference;
    /**
zYne's avatar
zYne committed
53
     * @var string $referenceField         the reference field of the collection
lsmith's avatar
lsmith committed
54
     */
zYne's avatar
zYne committed
55
    protected $referenceField;
lsmith's avatar
lsmith committed
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
    /**
     * @var Doctrine_Relation               the record this collection is related to, if any
     */
    protected $relation;
    /**
     * @var string $keyColumn               the name of the column that is used for collection key mapping
     */
    protected $keyColumn;
    /**
     * @var Doctrine_Null $null             used for extremely fast null value testing
     */
    protected static $null;


    /**
     * constructor
     *
     * @param Doctrine_Table|string $table
     */
75
    public function __construct($table, $keyColumn = null)
lsmith's avatar
lsmith committed
76 77 78 79 80
    {
        if ( ! ($table instanceof Doctrine_Table)) {
            $table = Doctrine_Manager::getInstance()
                        ->getTable($table);
        }
zYne's avatar
zYne committed
81
        $this->_table = $table;
lsmith's avatar
lsmith committed
82

83 84 85 86 87 88
        if ($keyColumn === null) {
            $keyColumn = $table->getBoundQueryPart('indexBy');
        }

        if ($keyColumn !== null) {
            $this->keyColumn = $keyColumn;
lsmith's avatar
lsmith committed
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
        }
    }
    /**
     * initNullObject
     * initializes the null object for this collection
     *
     * @return void
     */
    public static function initNullObject(Doctrine_Null $null)
    {
        self::$null = $null;
    }
    /**
     * getTable
     * returns the table this collection belongs to
     *
     * @return Doctrine_Table
     */
    public function getTable()
    {
zYne's avatar
zYne committed
109
        return $this->_table;
lsmith's avatar
lsmith committed
110
    }
zYne's avatar
zYne committed
111 112 113 114 115 116 117 118
    /**
     * setData
     *
     * @param array $data
     * @return Doctrine_Collection
     */
    public function setData(array $data) 
    {
119
        $this->data = $data;
zYne's avatar
zYne committed
120
    }
lsmith's avatar
lsmith committed
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
    /**
     * this method is automatically called when this Doctrine_Collection is serialized
     *
     * @return array
     */
    public function serialize()
    {
        $vars = get_object_vars($this);

        unset($vars['reference']);
        unset($vars['reference_field']);
        unset($vars['relation']);
        unset($vars['expandable']);
        unset($vars['expanded']);
        unset($vars['generator']);

zYne's avatar
zYne committed
137
        $vars['_table'] = $vars['_table']->getComponentName();
lsmith's avatar
lsmith committed
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157

        return serialize($vars);
    }
    /**
     * unseralize
     * this method is automatically called everytime a Doctrine_Collection object is unserialized
     *
     * @return void
     */
    public function unserialize($serialized)
    {
        $manager    = Doctrine_Manager::getInstance();
        $connection    = $manager->getCurrentConnection();

        $array = unserialize($serialized);

        foreach ($array as $name => $values) {
            $this->$name = $values;
        }

158
        $this->_table = $connection->getTable($this->_table);
lsmith's avatar
lsmith committed
159

160
        if ($keyColumn === null) {
zYne's avatar
zYne committed
161
            $keyColumn = $this->_table->getBoundQueryPart('indexBy');
162
        }
lsmith's avatar
lsmith committed
163

164 165
        if ($keyColumn !== null) {
            $this->keyColumn = $keyColumn;
lsmith's avatar
lsmith committed
166 167 168 169
        }
    }
    /**
     * setKeyColumn
zYne's avatar
zYne committed
170
     * sets the key column for this collection
lsmith's avatar
lsmith committed
171 172
     *
     * @param string $column
zYne's avatar
zYne committed
173
     * @return Doctrine_Collection
lsmith's avatar
lsmith committed
174 175 176 177
     */
    public function setKeyColumn($column)
    {
        $this->keyColumn = $column;
zYne's avatar
zYne committed
178 179
        
        return $this;
lsmith's avatar
lsmith committed
180 181 182 183 184 185 186 187 188 189 190 191
    }
    /**
     * getKeyColumn
     * returns the name of the key column
     *
     * @return string
     */
    public function getKeyColumn()
    {
        return $this->column;
    }
    /**
zYne's avatar
zYne committed
192
     * getData
lsmith's avatar
lsmith committed
193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226
     * returns all the records as an array
     *
     * @return array
     */
    public function getData()
    {
        return $this->data;
    }
    /**
     * getFirst
     * returns the first record in the collection
     *
     * @return mixed
     */
    public function getFirst()
    {
        return reset($this->data);
    }
    /**
     * getLast
     * returns the last record in the collection
     *
     * @return mixed
     */
    public function getLast()
    {
        return end($this->data);
    }
    /**
     * setReference
     * sets a reference pointer
     *
     * @return void
     */
227
    public function setReference(Doctrine_Record $record, Doctrine_Relation $relation)
lsmith's avatar
lsmith committed
228 229 230 231
    {
        $this->reference       = $record;
        $this->relation        = $relation;

232 233
        if ($relation instanceof Doctrine_Relation_ForeignKey || 
            $relation instanceof Doctrine_Relation_LocalKey) {
lsmith's avatar
lsmith committed
234

zYne's avatar
zYne committed
235
            $this->referenceField = $relation->getForeign();
lsmith's avatar
lsmith committed
236 237 238

            $value = $record->get($relation->getLocal());

zYne's avatar
zYne committed
239
            foreach ($this->data as $record) {
lsmith's avatar
lsmith committed
240
                if ($value !== null) {
zYne's avatar
zYne committed
241
                    $record->set($this->referenceField, $value, false);
lsmith's avatar
lsmith committed
242
                } else {
zYne's avatar
zYne committed
243
                    $record->set($this->referenceField, $this->reference, false);
lsmith's avatar
lsmith committed
244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283
                }
            }
        } elseif ($relation instanceof Doctrine_Relation_Association) {

        }
    }
    /**
     * getReference
     *
     * @return mixed
     */
    public function getReference()
    {
        return $this->reference;
    }
    /**
     * remove
     * removes a specified collection element
     *
     * @param mixed $key
     * @return boolean
     */
    public function remove($key)
    {
        $removed = $this->data[$key];

        unset($this->data[$key]);
        return $removed;
    }
    /**
     * contains
     * whether or not this collection contains a specified element
     *
     * @param mixed $key                    the key of the element
     * @return boolean
     */
    public function contains($key)
    {
        return isset($this->data[$key]);
    }
zYne's avatar
zYne committed
284 285 286 287
    public function search(Doctrine_Record $record)
    {
        return array_search($record, $this->data, true);
    }
lsmith's avatar
lsmith committed
288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306
    /**
     * get
     * returns a record for given key
     *
     * There are two special cases:
     *
     * 1. if null is given as a key a new record is created and attached
     * at the end of the collection
     *
     * 2. if given key does not exist, then a new record is create and attached
     * to the given key
     *
     * Collection also maps referential information to newly created records
     *
     * @param mixed $key                    the key of the element
     * @return Doctrine_Record              return a specified record
     */
    public function get($key)
    {
zYne's avatar
zYne committed
307
        if ( ! isset($this->data[$key])) {
308
            $record = $this->_table->create();
lsmith's avatar
lsmith committed
309

zYne's avatar
zYne committed
310
            if (isset($this->referenceField)) {
zYne's avatar
zYne committed
311 312 313 314 315 316 317
                $value = $this->reference->get($this->relation->getLocal());

                if ($value !== null) {
                    $record->set($this->referenceField, $value, false);
                } else {
                    $record->set($this->referenceField, $this->reference, false);
                }
lsmith's avatar
lsmith committed
318
            }
zYne's avatar
zYne committed
319 320 321 322 323
            if ($key === null) {
                $this->data[] = $record;
            } else {
                $this->data[$key] = $record;      	
            }
lsmith's avatar
lsmith committed
324

325 326 327 328 329
            if (isset($this->keyColumn)) {

                $record->set($this->keyColumn, $key);
            }

lsmith's avatar
lsmith committed
330
            return $record;
331
        }
lsmith's avatar
lsmith committed
332 333 334 335 336 337 338 339 340 341

        return $this->data[$key];
    }

    /**
     * @return array                an array containing all primary keys
     */
    public function getPrimaryKeys()
    {
        $list = array();
zYne's avatar
zYne committed
342
        $name = $this->_table->getIdentifier();
lsmith's avatar
lsmith committed
343 344 345 346 347 348 349

        foreach ($this->data as $record) {
            if (is_array($record) && isset($record[$name])) {
                $list[] = $record[$name];
            } else {
                $list[] = $record->getIncremented();
            }
zYne's avatar
zYne committed
350
        }
lsmith's avatar
lsmith committed
351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379
        return $list;
    }
    /**
     * returns all keys
     * @return array
     */
    public function getKeys()
    {
        return array_keys($this->data);
    }
    /**
     * count
     * this class implements interface countable
     * returns the number of records in this collection
     *
     * @return integer
     */
    public function count()
    {
        return count($this->data);
    }
    /**
     * set
     * @param integer $key
     * @param Doctrine_Record $record
     * @return void
     */
    public function set($key, Doctrine_Record $record)
    {
zYne's avatar
zYne committed
380 381
        if (isset($this->referenceField)) {
            $record->set($this->referenceField, $this->reference, false);
lsmith's avatar
lsmith committed
382
        }
383

lsmith's avatar
lsmith committed
384 385 386 387 388 389 390 391
        $this->data[$key] = $record;
    }
    /**
     * adds a record to collection
     * @param Doctrine_Record $record              record to be added
     * @param string $key                          optional key for the record
     * @return boolean
     */
zYne's avatar
zYne committed
392
    public function add(Doctrine_Record $record, $key = null)
lsmith's avatar
lsmith committed
393
    {
zYne's avatar
zYne committed
394
        if (isset($this->referenceField)) {
zYne's avatar
zYne committed
395 396 397 398 399 400 401
            $value = $this->reference->get($this->relation->getLocal());

            if ($value !== null) {
                $record->set($this->referenceField, $value, false);
            } else {
                $record->set($this->referenceField, $this->reference, false);
            }
lsmith's avatar
lsmith committed
402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440
        }
        /**
         * for some weird reason in_array cannot be used here (php bug ?)
         *
         * if used it results in fatal error : [ nesting level too deep ]
         */
        foreach ($this->data as $val) {
            if ($val === $record) {
                return false;
            }
        }

        if (isset($key)) {
            if (isset($this->data[$key])) {
                return false;
            }
            $this->data[$key] = $record;
            return true;
        }

        if (isset($this->keyColumn)) {
            $value = $record->get($this->keyColumn);
            if ($value === null) {
                throw new Doctrine_Collection_Exception("Couldn't create collection index. Record field '".$this->keyColumn."' was null.");
            }
            $this->data[$value] = $record;
        } else {
            $this->data[] = $record;
        }
        return true;
    }
    /**
     * loadRelated
     *
     * @param mixed $name
     * @return boolean
     */
    public function loadRelated($name = null)
    {
runa's avatar
runa committed
441
        $list = array();
zYne's avatar
zYne committed
442
        $query   = new Doctrine_Query($this->_table->getConnection());
lsmith's avatar
lsmith committed
443 444 445 446 447 448 449

        if ( ! isset($name)) {
            foreach ($this->data as $record) {
                $value = $record->getIncremented();
                if ($value !== null) {
                    $list[] = $value;
                }
450
            }
zYne's avatar
zYne committed
451 452
            $query->from($this->_table->getComponentName() . '(' . implode(", ",$this->_table->getPrimaryKeys()) . ')');
            $query->where($this->_table->getComponentName() . '.id IN (' . substr(str_repeat("?, ", count($list)),0,-2) . ')');
lsmith's avatar
lsmith committed
453 454 455 456

            return $query;
        }

zYne's avatar
zYne committed
457
        $rel     = $this->_table->getRelation($name);
lsmith's avatar
lsmith committed
458 459 460

        if ($rel instanceof Doctrine_Relation_LocalKey || $rel instanceof Doctrine_Relation_ForeignKey) {
            foreach ($this->data as $record) {
461 462
                $list[] = $record[$rel->getLocal()];
            }
lsmith's avatar
lsmith committed
463 464 465 466 467 468
        } else {
            foreach ($this->data as $record) {
                $value = $record->getIncremented();
                if ($value !== null) {
                    $list[] = $value;
                }
469
            }
lsmith's avatar
lsmith committed
470
        }
471

lsmith's avatar
lsmith committed
472 473 474 475 476 477 478 479 480 481 482 483 484 485 486
        $dql     = $rel->getRelationDql(count($list), 'collection');

        $coll    = $query->query($dql, $list);

        $this->populateRelated($name, $coll);
    }
    /**
     * populateRelated
     *
     * @param string $name
     * @param Doctrine_Collection $coll
     * @return void
     */
    public function populateRelated($name, Doctrine_Collection $coll)
    {
zYne's avatar
zYne committed
487
        $rel     = $this->_table->getRelation($name);
lsmith's avatar
lsmith committed
488 489 490 491 492 493 494 495 496 497 498 499 500 501
        $table   = $rel->getTable();
        $foreign = $rel->getForeign();
        $local   = $rel->getLocal();

        if ($rel instanceof Doctrine_Relation_LocalKey) {
            foreach ($this->data as $key => $record) {
                foreach ($coll as $k => $related) {
                    if ($related[$foreign] == $record[$local]) {
                        $this->data[$key]->setRelated($name, $related);
                    }
                }
            }
        } elseif ($rel instanceof Doctrine_Relation_ForeignKey) {
            foreach ($this->data as $key => $record) {
zYne's avatar
zYne committed
502
                if ( ! $record->exists()) {
lsmith's avatar
lsmith committed
503 504 505 506 507 508 509 510 511 512 513 514 515 516
                    continue;
                }
                $sub = new Doctrine_Collection($table);

                foreach ($coll as $k => $related) {
                    if ($related[$foreign] == $record[$local]) {
                        $sub->add($related);
                        $coll->remove($k);
                    }
                }

                $this->data[$key]->setRelated($name, $sub);
            }
        } elseif ($rel instanceof Doctrine_Relation_Association) {
zYne's avatar
zYne committed
517
            $identifier = $this->_table->getIdentifier();
lsmith's avatar
lsmith committed
518 519 520 521
            $asf        = $rel->getAssociationFactory();
            $name       = $table->getComponentName();

            foreach ($this->data as $key => $record) {
zYne's avatar
zYne committed
522
                if ( ! $record->exists()) {
lsmith's avatar
lsmith committed
523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545
                    continue;
                }
                $sub = new Doctrine_Collection($table);
                foreach ($coll as $k => $related) {
                    if ($related->get($local) == $record[$identifier]) {
                        $sub->add($related->get($name));
                    }
                }
                $this->data[$key]->setRelated($name, $sub);

            }
        }
    }
    /**
     * getNormalIterator
     * returns normal iterator - an iterator that will not expand this collection
     *
     * @return Doctrine_Iterator_Normal
     */
    public function getNormalIterator()
    {
        return new Doctrine_Collection_Iterator_Normal($this);
    }
zYne's avatar
zYne committed
546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582
    /**
     * takeSnapshot
     * takes a snapshot from this collection
     *
     * snapshots are used for diff processing, for example
     * when a fetched collection has three elements, then two of those
     * are being removed the diff would contain one element
     *
     * Doctrine_Collection::save() attaches the diff with the help of last
     * snapshot.
     *
     * @return Doctrine_Collection
     */
    public function takeSnapshot()
    {
        $this->_snapshot = $this->data;
        
        return $this;
    }
    /**
     * getSnapshot
     * returns the data of the last snapshot
     *
     * @return array    returns the data in last snapshot
     */
    public function getSnapshot()
    {
        return $this->_snapshot;
    }
    /**
     * processDiff
     * processes the difference of the last snapshot and the current data
     *
     * an example:
     * Snapshot with the objects 1, 2 and 4
     * Current data with objects 2, 3 and 5
     *
zYne's avatar
zYne committed
583
     * The process would remove object 4
zYne's avatar
zYne committed
584 585 586 587 588
     *
     * @return Doctrine_Collection
     */
    public function processDiff() 
    {
589
        foreach (array_udiff($this->_snapshot, $this->data, array($this, "compareRecords")) as $record) {
zYne's avatar
zYne committed
590 591
            $record->delete();
        }
zYne's avatar
zYne committed
592

zYne's avatar
zYne committed
593 594
        return $this;
    }
595 596 597 598 599 600
    /**
     * toArray
     * Mimics the result of a $query->execute(array(), Doctrine::FETCH_ARRAY);
     *
     * @param boolean $deep
     */
601
    public function toArray($deep = false, $prefixKey = false)
zYne's avatar
zYne committed
602
    {
603 604 605 606 607 608
        $data = array();
        foreach ($this->data as $key => $record) {
            
            $key = $prefixKey ? get_class($record) . '_' .$key:$key;
            
            $data[$key] = $record->toArray($deep, $prefixKey);
609
        }
610 611
        
        return $data;
zYne's avatar
zYne committed
612
    }
613 614 615
    public function fromArray($array)
    {
        $data = array();
616
        foreach ($array as $row) {
617 618 619
            $record = $this->_table->getRecord();
            $record->fromArray($row);
            
620
            $data[] = $record;
621 622 623 624
        }
        
        $this->data = $data;
    }
625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640
    public function exportTo($type, $deep = false)
    {
        if ($type == 'array') {
            return $this->toArray($deep);
        } else {
            return Doctrine_Parser::dump($this->toArray($deep, true), $type);
        }
    }
    public function importFrom($type, $data)
    {
        if ($type == 'array') {
            return $this->fromArray($data);
        } else {
            return $this->fromArray(Doctrine_Parser::load($data, $type));
        }
    }
zYne's avatar
zYne committed
641 642
    public function getDeleteDiff()
    {
643
        return array_udiff($this->_snapshot, $this->data, array($this, "compareRecords"));
zYne's avatar
zYne committed
644 645 646
    }
    public function getInsertDiff()
    {
647 648 649 650 651 652 653 654 655 656
        return array_udiff($this->data, $this->_snapshot, array($this, "compareRecords"));
    }
    /**
     * compareRecords
     * Compares two records. To be used on _snapshot diffs using array_udiff
     */
    protected function compareRecords($a, $b)
    {
        if ($a->getOid() == $b->getOid()) return 0;
        return ($a->getOid() > $b->getOid()) ? 1 : -1;
zYne's avatar
zYne committed
657
    }
lsmith's avatar
lsmith committed
658 659
    /**
     * save
zYne's avatar
zYne committed
660 661
     * saves all records of this collection and processes the 
     * difference of the last snapshot and the current data
lsmith's avatar
lsmith committed
662
     *
663
     * @param Doctrine_Connection $conn     optional connection parameter
zYne's avatar
zYne committed
664
     * @return Doctrine_Collection
lsmith's avatar
lsmith committed
665 666 667 668
     */
    public function save(Doctrine_Connection $conn = null)
    {
        if ($conn == null) {
zYne's avatar
zYne committed
669
            $conn = $this->_table->getConnection();
lsmith's avatar
lsmith committed
670 671
        }
        $conn->beginTransaction();
672

673
        $conn->transaction->addCollection($this);
lsmith's avatar
lsmith committed
674

zYne's avatar
zYne committed
675 676
        $this->processDiff();

677
        foreach ($this->getData() as $key => $record) {
678
            $record->save($conn);
zYne's avatar
zYne committed
679
        }
lsmith's avatar
lsmith committed
680 681

        $conn->commit();
682

zYne's avatar
zYne committed
683
        return $this;
lsmith's avatar
lsmith committed
684 685
    }
    /**
zYne's avatar
zYne committed
686
     * delete
lsmith's avatar
lsmith committed
687 688 689 690
     * single shot delete
     * deletes all records from this collection
     * and uses only one database query to perform this operation
     *
zYne's avatar
zYne committed
691
     * @return Doctrine_Collection
lsmith's avatar
lsmith committed
692 693 694 695
     */
    public function delete(Doctrine_Connection $conn = null)
    {
        if ($conn == null) {
zYne's avatar
zYne committed
696
            $conn = $this->_table->getConnection();
lsmith's avatar
lsmith committed
697 698 699
        }

        $conn->beginTransaction();
700
        $conn->transaction->addCollection($this);
lsmith's avatar
lsmith committed
701 702

        foreach ($this as $key => $record) {
703
            $record->delete($conn);
lsmith's avatar
lsmith committed
704 705 706 707 708
        }

        $conn->commit();

        $this->data = array();
zYne's avatar
zYne committed
709 710
        
        return $this;
lsmith's avatar
lsmith committed
711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727
    }
    /**
     * getIterator
     * @return object ArrayIterator
     */
    public function getIterator()
    {
        $data = $this->data;
        return new ArrayIterator($data);
    }
    /**
     * returns a string representation of this object
     */
    public function __toString()
    {
        return Doctrine_Lib::getCollectionAsString($this);
    }
728
}