Batch.php 6.06 KB
Newer Older
doctrine's avatar
doctrine committed
1
<?php
zYne's avatar
zYne committed
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/*
 *  $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>.
 */
doctrine's avatar
doctrine committed
21 22 23 24 25 26
Doctrine::autoload('Doctrine_Collection');
/**
 * Doctrine_Collection_Batch       a collection of records,
 *                                 with batch load strategy
 *
 *
lsmith's avatar
lsmith committed
27 28 29 30 31 32 33 34
 * @author      Konsta Vesterinen <kvesteri@cc.hut.fi>
 * @package     Doctrine
 * @license     http://www.opensource.org/licenses/lgpl-license.php LGPL
 * @version     $Revision$
 * @category    Object Relational Mapping
 * @link        www.phpdoctrine.com
 * @since       1.0
 */
lsmith's avatar
lsmith committed
35 36
class Doctrine_Collection_Batch extends Doctrine_Collection
{
doctrine's avatar
doctrine committed
37 38 39 40 41 42 43 44
    /**
     * @var integer $batchSize      batch size
     */
    private $batchSize;
    /**
     * @var array $loaded           an array containing the loaded batches, keys representing the batch indexes
     */
    private $loaded = array();
lsmith's avatar
lsmith committed
45

lsmith's avatar
lsmith committed
46 47
    public function __construct(Doctrine_Table $table)
    {
doctrine's avatar
doctrine committed
48 49 50 51 52 53 54 55
        parent::__construct($table);
        $this->batchSize = $this->getTable()->getAttribute(Doctrine::ATTR_BATCH_SIZE);
    }

    /**
     * @param integer $batchSize    batch size
     * @return boolean
     */
lsmith's avatar
lsmith committed
56 57
    public function setBatchSize($batchSize)
    {
doctrine's avatar
doctrine committed
58
        $batchSize = (int) $batchSize;
lsmith's avatar
lsmith committed
59
        if ($batchSize <= 0) {
doctrine's avatar
doctrine committed
60
            return false;
lsmith's avatar
lsmith committed
61
        }
doctrine's avatar
doctrine committed
62 63 64 65 66 67 68 69
        $this->batchSize = $batchSize;
        return true;
    }
    /**
     * returns the batch size of this collection
     *
     * @return integer
     */
lsmith's avatar
lsmith committed
70 71
    public function getBatchSize()
    {
doctrine's avatar
doctrine committed
72 73 74
        return $this->batchSize;
    }
    /**
lsmith's avatar
lsmith committed
75
     * load
doctrine's avatar
doctrine committed
76 77 78 79 80
     * loads a specified element, by loading the batch the element is part of
     *
     * @param Doctrine_Record $record              record to be loaded
     * @return boolean                             whether or not the load operation was successful
     */
lsmith's avatar
lsmith committed
81 82
    public function load(Doctrine_Record $record)
    {
lsmith's avatar
lsmith committed
83
        if (empty($this->data)) {
doctrine's avatar
doctrine committed
84
            return false;
lsmith's avatar
lsmith committed
85
        }
86
        $id  = $record->obtainIdentifier();
doctrine's avatar
doctrine committed
87
        $identifier = $this->table->getIdentifier();
lsmith's avatar
lsmith committed
88 89 90
        foreach ($this->data as $key => $v) {
            if (is_object($v)) {
                if ($v->obtainIdentifier() == $id) {
doctrine's avatar
doctrine committed
91
                    break;
lsmith's avatar
lsmith committed
92 93 94
                }
            } elseif (is_array($v[$identifier])) {
                if ($v[$identifier] == $id) {
doctrine's avatar
doctrine committed
95
                    break;
lsmith's avatar
lsmith committed
96
                }
doctrine's avatar
doctrine committed
97 98 99 100
            }
        }
        $x = floor($key / $this->batchSize);

lsmith's avatar
lsmith committed
101
        if ( ! isset($this->loaded[$x])) {
doctrine's avatar
doctrine committed
102 103 104 105 106 107
            $e  = $x * $this->batchSize;
            $e2 = ($x + 1)* $this->batchSize;

            $a       = array();
            $proxies = array();

lsmith's avatar
lsmith committed
108 109
            for ($i = $e; $i < $e2 && $i < $this->count(); $i++) {
                if ($this->data[$i] instanceof Doctrine_Record) {
110
                    $id = $this->data[$i]->getIncremented();
lsmith's avatar
lsmith committed
111
                } elseif (is_array($this->data[$i])) {
doctrine's avatar
doctrine committed
112
                    $id = $this->data[$i][$identifier];
lsmith's avatar
lsmith committed
113
                }
doctrine's avatar
doctrine committed
114
                $a[$i] = $id;
lsmith's avatar
lsmith committed
115
            };
doctrine's avatar
doctrine committed
116 117 118 119 120 121 122 123 124

            $c = count($a);

            $pk     = $this->table->getPrimaryKeys();
            $query  = $this->table->getQuery()." WHERE ";
            $query .= ($c > 1)?$identifier." IN (":$pk[0]." = ";
            $query .= substr(str_repeat("?, ",count($a)),0,-2);
            $query .= ($c > 1)?") ORDER BY ".$pk[0]." ASC":"";

zYne's avatar
zYne committed
125
            $stmt  = $this->table->getConnection()->execute($query,array_values($a));
doctrine's avatar
doctrine committed
126

lsmith's avatar
lsmith committed
127
             foreach ($a as $k => $id) {
doctrine's avatar
doctrine committed
128 129
                $row = $stmt->fetch(PDO::FETCH_ASSOC);

lsmith's avatar
lsmith committed
130
                if ($row === false) {
doctrine's avatar
doctrine committed
131
                    break;
lsmith's avatar
lsmith committed
132
                }
doctrine's avatar
doctrine committed
133
                $this->table->setData($row);
lsmith's avatar
lsmith committed
134
                if (is_object($this->data[$k])) {
doctrine's avatar
doctrine committed
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
                    $this->data[$k]->factoryRefresh($this->table);
                } else {
                    $this->data[$k] = $this->table->getRecord();
                }

            }

            $this->loaded[$x] = true;
            return true;
        } else {
            return false;
        }
    }

    /**
     * get
     * @param mixed $key                the key of the record
     * @return object Doctrine_Record               record
     */
lsmith's avatar
lsmith committed
154 155
    public function get($key)
    {
lsmith's avatar
lsmith committed
156 157
        if (isset($this->data[$key])) {
            switch (gettype($this->data[$key])) {
158 159 160 161
                case "array":
                    // Doctrine_Record didn't exist in cache
                    $this->table->setData($this->data[$key]);
                    $this->data[$key] = $this->table->getProxy();
lsmith's avatar
lsmith committed
162

163 164
                    $this->data[$key]->addCollection($this);
                    break;
lsmith's avatar
lsmith committed
165
            };
doctrine's avatar
doctrine committed
166 167 168
        } else {
            $this->expand($key);

lsmith's avatar
lsmith committed
169
            if ( ! isset($this->data[$key])) {
doctrine's avatar
doctrine committed
170
                $this->data[$key] = $this->table->create();
lsmith's avatar
lsmith committed
171
            }
doctrine's avatar
doctrine committed
172 173
        }

lsmith's avatar
lsmith committed
174
        if (isset($this->reference_field)) {
zYne's avatar
zYne committed
175
            $this->data[$key]->set($this->reference_field, $this->reference, false);
lsmith's avatar
lsmith committed
176
        }
doctrine's avatar
doctrine committed
177 178 179 180 181
        return $this->data[$key];
    }
    /**
     * @return Doctrine_Iterator
     */
lsmith's avatar
lsmith committed
182 183
    public function getIterator()
    {
184
        return new Doctrine_Collection_Iterator_Expandable($this);
doctrine's avatar
doctrine committed
185 186
    }
}