CakePHP
  • Documentation
    • Book
    • API
    • Videos
    • Logos & Trademarks
  • Business Solutions
  • Swag
  • Road Trip
  • Team
  • Community
    • Community
    • Team
    • Issues (Github)
    • YouTube Channel
    • Get Involved
    • Bakery
    • Featured Resources
    • Newsletter
    • Certification
    • My CakePHP
    • CakeFest
    • Facebook
    • Twitter
    • Help & Support
    • Forum
    • Stack Overflow
    • IRC
    • Slack
    • Paid Support
CakePHP

C CakePHP 3.8 Red Velvet API

  • Overview
  • Tree
  • Deprecated
  • Version:
    • 3.8
      • 3.8
      • 3.7
      • 3.6
      • 3.5
      • 3.4
      • 3.3
      • 3.2
      • 3.1
      • 3.0
      • 2.10
      • 2.9
      • 2.8
      • 2.7
      • 2.6
      • 2.5
      • 2.4
      • 2.3
      • 2.2
      • 2.1
      • 2.0
      • 1.3
      • 1.2

Namespaces

  • Cake
    • Auth
      • Storage
    • Cache
      • Engine
    • Collection
      • Iterator
    • Command
    • Console
      • Exception
    • Controller
      • Component
      • Exception
    • Core
      • Configure
        • Engine
      • Exception
      • Retry
    • Database
      • Driver
      • Exception
      • Expression
      • Schema
      • Statement
      • Type
    • Datasource
      • Exception
    • Error
      • Middleware
    • Event
      • Decorator
    • Filesystem
    • Form
    • Http
      • Client
        • Adapter
        • Auth
      • Cookie
      • Exception
      • Middleware
      • Session
    • I18n
      • Formatter
      • Middleware
      • Parser
    • Log
      • Engine
    • Mailer
      • Exception
      • Transport
    • Network
      • Exception
    • ORM
      • Association
      • Behavior
        • Translate
      • Exception
      • Locator
      • Rule
    • Routing
      • Exception
      • Filter
      • Middleware
      • Route
    • Shell
      • Helper
      • Task
    • TestSuite
      • Fixture
      • Stub
    • Utility
      • Exception
    • Validation
    • View
      • Exception
      • Form
      • Helper
      • Widget
  • None

Classes

  • BufferedStatement
  • CallbackStatement
  • PDOStatement
  • StatementDecorator
  1: <?php
  2: /**
  3:  * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  4:  * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  5:  *
  6:  * Licensed under The MIT License
  7:  * For full copyright and license information, please see the LICENSE.txt
  8:  * Redistributions of files must retain the above copyright notice.
  9:  *
 10:  * @copyright     Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
 11:  * @link          https://cakephp.org CakePHP(tm) Project
 12:  * @since         3.0.0
 13:  * @license       https://opensource.org/licenses/mit-license.php MIT License
 14:  */
 15: namespace Cake\Database\Statement;
 16: 
 17: use Cake\Database\StatementInterface;
 18: use Cake\Database\TypeConverterTrait;
 19: use Iterator;
 20: 
 21: /**
 22:  * A statement decorator that implements buffered results.
 23:  *
 24:  * This statement decorator will save fetched results in memory, allowing
 25:  * the iterator to be rewound and reused.
 26:  */
 27: class BufferedStatement implements Iterator, StatementInterface
 28: {
 29:     use TypeConverterTrait;
 30: 
 31:     /**
 32:      * If true, all rows were fetched
 33:      *
 34:      * @var bool
 35:      */
 36:     protected $_allFetched = false;
 37: 
 38:     /**
 39:      * The decorated statement
 40:      *
 41:      * @var \Cake\Database\StatementInterface
 42:      */
 43:     protected $statement;
 44: 
 45:     /**
 46:      * The driver for the statement
 47:      *
 48:      * @var \Cake\Database\DriverInterface
 49:      */
 50:     protected $_driver;
 51: 
 52:     /**
 53:      * The in-memory cache containing results from previous iterators
 54:      *
 55:      * @var array
 56:      */
 57:     protected $buffer = [];
 58: 
 59:     /**
 60:      * Whether or not this statement has already been executed
 61:      *
 62:      * @var bool
 63:      */
 64:     protected $_hasExecuted = false;
 65: 
 66:     /**
 67:      * The current iterator index.
 68:      *
 69:      * @var int
 70:      */
 71:     protected $index = 0;
 72: 
 73:     /**
 74:      * Constructor
 75:      *
 76:      * @param \Cake\Database\StatementInterface $statement Statement implementation such as PDOStatement
 77:      * @param \Cake\Database\Driver $driver Driver instance
 78:      */
 79:     public function __construct(StatementInterface $statement, $driver)
 80:     {
 81:         $this->statement = $statement;
 82:         $this->_driver = $driver;
 83:     }
 84: 
 85:     /**
 86:      * Magic getter to return $queryString as read-only.
 87:      *
 88:      * @param string $property internal property to get
 89:      * @return mixed
 90:      */
 91:     public function __get($property)
 92:     {
 93:         if ($property === 'queryString') {
 94:             return $this->statement->queryString;
 95:         }
 96:     }
 97: 
 98:     /**
 99:      * {@inheritDoc}
100:      */
101:     public function bindValue($column, $value, $type = 'string')
102:     {
103:         $this->statement->bindValue($column, $value, $type);
104:     }
105: 
106:     /**
107:      * {@inheritDoc}
108:      */
109:     public function closeCursor()
110:     {
111:         $this->statement->closeCursor();
112:     }
113: 
114:     /**
115:      * {@inheritDoc}
116:      */
117:     public function columnCount()
118:     {
119:         return $this->statement->columnCount();
120:     }
121: 
122:     /**
123:      * {@inheritDoc}
124:      */
125:     public function errorCode()
126:     {
127:         return $this->statement->errorCode();
128:     }
129: 
130:     /**
131:      * {@inheritDoc}
132:      */
133:     public function errorInfo()
134:     {
135:         return $this->statement->errorInfo();
136:     }
137: 
138:     /**
139:      * {@inheritDoc}
140:      */
141:     public function execute($params = null)
142:     {
143:         $this->_reset();
144:         $this->_hasExecuted = true;
145: 
146:         return $this->statement->execute($params);
147:     }
148: 
149:     /**
150:      * {@inheritDoc}
151:      */
152:     public function fetchColumn($position)
153:     {
154:         $result = $this->fetch(static::FETCH_TYPE_NUM);
155:         if (isset($result[$position])) {
156:             return $result[$position];
157:         }
158: 
159:         return false;
160:     }
161: 
162:     /**
163:      * Statements can be passed as argument for count() to return the number
164:      * for affected rows from last execution.
165:      *
166:      * @return int
167:      */
168:     public function count()
169:     {
170:         return $this->rowCount();
171:     }
172: 
173:     /**
174:      * {@inheritDoc}
175:      */
176:     public function bind($params, $types)
177:     {
178:         $this->statement->bind($params, $types);
179:     }
180: 
181:     /**
182:      * {@inheritDoc}
183:      */
184:     public function lastInsertId($table = null, $column = null)
185:     {
186:         return $this->statement->lastInsertId($table, $column);
187:     }
188: 
189:     /**
190:      * {@inheritDoc}
191:      *
192:      * @param string $type The type to fetch.
193:      * @return array|false
194:      */
195:     public function fetch($type = self::FETCH_TYPE_NUM)
196:     {
197:         if ($this->_allFetched) {
198:             $row = false;
199:             if (isset($this->buffer[$this->index])) {
200:                 $row = $this->buffer[$this->index];
201:             }
202:             $this->index += 1;
203: 
204:             if ($row && $type === static::FETCH_TYPE_NUM) {
205:                 return array_values($row);
206:             }
207: 
208:             return $row;
209:         }
210: 
211:         $record = $this->statement->fetch($type);
212:         if ($record === false) {
213:             $this->_allFetched = true;
214:             $this->statement->closeCursor();
215: 
216:             return false;
217:         }
218:         $this->buffer[] = $record;
219: 
220:         return $record;
221:     }
222: 
223:     /**
224:      * {@inheritdoc}
225:      */
226:     public function fetchAssoc()
227:     {
228:         $result = $this->fetch(static::FETCH_TYPE_ASSOC);
229: 
230:         return $result ?: [];
231:     }
232: 
233:     /**
234:      * {@inheritDoc}
235:      *
236:      * @param string $type The type to fetch.
237:      * @return array
238:      */
239:     public function fetchAll($type = self::FETCH_TYPE_NUM)
240:     {
241:         if ($this->_allFetched) {
242:             return $this->buffer;
243:         }
244:         $results = $this->statement->fetchAll($type);
245:         if ($results !== false) {
246:             $this->buffer = array_merge($this->buffer, $results);
247:         }
248:         $this->_allFetched = true;
249:         $this->statement->closeCursor();
250: 
251:         return $this->buffer;
252:     }
253: 
254:     /**
255:      * {@inheritDoc}
256:      */
257:     public function rowCount()
258:     {
259:         if (!$this->_allFetched) {
260:             $this->fetchAll(static::FETCH_TYPE_ASSOC);
261:         }
262: 
263:         return count($this->buffer);
264:     }
265: 
266:     /**
267:      * Reset all properties
268:      *
269:      * @return void
270:      */
271:     protected function _reset()
272:     {
273:         $this->buffer = [];
274:         $this->_allFetched = false;
275:         $this->index = 0;
276:     }
277: 
278:     /**
279:      * Returns the current key in the iterator
280:      *
281:      * @return mixed
282:      */
283:     public function key()
284:     {
285:         return $this->index;
286:     }
287: 
288:     /**
289:      * Returns the current record in the iterator
290:      *
291:      * @return mixed
292:      */
293:     public function current()
294:     {
295:         return $this->buffer[$this->index];
296:     }
297: 
298:     /**
299:      * Rewinds the collection
300:      *
301:      * @return void
302:      */
303:     public function rewind()
304:     {
305:         $this->index = 0;
306:     }
307: 
308:     /**
309:      * Returns whether or not the iterator has more elements
310:      *
311:      * @return bool
312:      */
313:     public function valid()
314:     {
315:         $old = $this->index;
316:         $row = $this->fetch(self::FETCH_TYPE_ASSOC);
317: 
318:         // Restore the index as fetch() increments during
319:         // the cache scenario.
320:         $this->index = $old;
321: 
322:         return $row !== false;
323:     }
324: 
325:     /**
326:      * Advances the iterator pointer to the next element
327:      *
328:      * @return void
329:      */
330:     public function next()
331:     {
332:         $this->index += 1;
333:     }
334: 
335:     /**
336:      * Get the wrapped statement
337:      *
338:      * @return \Cake\Database\StatementInterface
339:      */
340:     public function getInnerStatement()
341:     {
342:         return $this->statement;
343:     }
344: }
345: 
Follow @CakePHP
#IRC
OpenHub
Rackspace
  • Business Solutions
  • Showcase
  • Documentation
  • Book
  • API
  • Videos
  • Logos & Trademarks
  • Community
  • Team
  • Issues (Github)
  • YouTube Channel
  • Get Involved
  • Bakery
  • Featured Resources
  • Newsletter
  • Certification
  • My CakePHP
  • CakeFest
  • Facebook
  • Twitter
  • Help & Support
  • Forum
  • Stack Overflow
  • IRC
  • Slack
  • Paid Support

Generated using CakePHP API Docs