1 | <?php |
||
31 | class BatchQueryResult extends BaseObject implements \Iterator |
||
32 | { |
||
33 | /** |
||
34 | * @var Connection the DB connection to be used when performing batch query. |
||
35 | * If null, the "db" application component will be used. |
||
36 | */ |
||
37 | public $db; |
||
38 | /** |
||
39 | * @var Query the query object associated with this batch query. |
||
40 | * Do not modify this property directly unless after [[reset()]] is called explicitly. |
||
41 | */ |
||
42 | public $query; |
||
43 | /** |
||
44 | * @var int the number of rows to be returned in each batch. |
||
45 | */ |
||
46 | public $batchSize = 100; |
||
47 | /** |
||
48 | * @var bool whether to return a single row during each iteration. |
||
49 | * If false, a whole batch of rows will be returned in each iteration. |
||
50 | */ |
||
51 | public $each = false; |
||
52 | |||
53 | /** |
||
54 | * @var DataReader the data reader associated with this batch query. |
||
55 | */ |
||
56 | private $_dataReader; |
||
57 | /** |
||
58 | * @var array the data retrieved in the current batch |
||
59 | */ |
||
60 | private $_batch; |
||
61 | /** |
||
62 | * @var mixed the value for the current iteration |
||
63 | */ |
||
64 | private $_value; |
||
65 | /** |
||
66 | * @var string|int the key for the current iteration |
||
67 | */ |
||
68 | private $_key; |
||
69 | |||
70 | |||
71 | /** |
||
72 | * Destructor. |
||
73 | */ |
||
74 | 6 | public function __destruct() |
|
79 | |||
80 | /** |
||
81 | * Resets the batch query. |
||
82 | * This method will clean up the existing batch query so that a new batch query can be performed. |
||
83 | */ |
||
84 | 6 | public function reset() |
|
85 | { |
||
86 | 6 | if ($this->_dataReader !== null) { |
|
87 | 6 | $this->_dataReader->close(); |
|
88 | } |
||
89 | 6 | $this->_dataReader = null; |
|
90 | 6 | $this->_batch = null; |
|
|
|||
91 | 6 | $this->_value = null; |
|
92 | 6 | $this->_key = null; |
|
93 | 6 | } |
|
94 | |||
95 | /** |
||
96 | * Resets the iterator to the initial state. |
||
97 | * This method is required by the interface [[\Iterator]]. |
||
98 | */ |
||
99 | 6 | public function rewind() |
|
104 | |||
105 | /** |
||
106 | * Moves the internal pointer to the next dataset. |
||
107 | * This method is required by the interface [[\Iterator]]. |
||
108 | */ |
||
109 | 6 | public function next() |
|
110 | { |
||
111 | 6 | if ($this->_batch === null || !$this->each || $this->each && next($this->_batch) === false) { |
|
112 | 6 | $this->_batch = $this->fetchData(); |
|
113 | 6 | reset($this->_batch); |
|
114 | } |
||
115 | |||
116 | 6 | if ($this->each) { |
|
117 | 3 | $this->_value = current($this->_batch); |
|
118 | 3 | if ($this->query->indexBy !== null) { |
|
119 | 3 | $this->_key = key($this->_batch); |
|
120 | 3 | } elseif (key($this->_batch) !== null) { |
|
121 | 3 | $this->_key = $this->_key === null ? 0 : $this->_key + 1; |
|
122 | } else { |
||
123 | 3 | $this->_key = null; |
|
124 | } |
||
125 | } else { |
||
126 | 6 | $this->_value = $this->_batch; |
|
127 | 6 | $this->_key = $this->_key === null ? 0 : $this->_key + 1; |
|
128 | } |
||
129 | 6 | } |
|
130 | |||
131 | /** |
||
132 | * Fetches the next batch of data. |
||
133 | * @return array the data fetched |
||
134 | */ |
||
135 | 6 | protected function fetchData() |
|
136 | { |
||
137 | 6 | if ($this->_dataReader === null) { |
|
138 | 6 | $this->_dataReader = $this->query->createCommand($this->db)->query(); |
|
139 | } |
||
140 | |||
141 | 6 | $rows = []; |
|
142 | 6 | $count = 0; |
|
143 | 6 | while ($count++ < $this->batchSize && ($row = $this->_dataReader->read())) { |
|
144 | 6 | $rows[] = $row; |
|
145 | } |
||
146 | |||
147 | 6 | return $this->query->populate($rows); |
|
148 | } |
||
149 | |||
150 | /** |
||
151 | * Returns the index of the current dataset. |
||
152 | * This method is required by the interface [[\Iterator]]. |
||
153 | * @return int the index of the current row. |
||
154 | */ |
||
155 | 3 | public function key() |
|
159 | |||
160 | /** |
||
161 | * Returns the current dataset. |
||
162 | * This method is required by the interface [[\Iterator]]. |
||
163 | * @return mixed the current dataset. |
||
164 | */ |
||
165 | 6 | public function current() |
|
169 | |||
170 | /** |
||
171 | * Returns whether there is a valid dataset at the current position. |
||
172 | * This method is required by the interface [[\Iterator]]. |
||
173 | * @return bool whether there is a valid dataset at the current position. |
||
174 | */ |
||
175 | 6 | public function valid() |
|
179 | } |
||
180 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..