5 use RedBeanPHP\Cursor as Cursor;
6 use RedBeanPHP\Repository as Repository;
11 * The BeanCollection represents a collection of beans and
12 * makes it possible to use database cursors. The BeanCollection
13 * has a method next() to obtain the first, next and last bean
14 * in the collection. The BeanCollection does not implement the array
15 * interface nor does it try to act like an array because it cannot go
16 * backward or rewind itself.
18 * Use the BeanCollection for large datasets where skip/limit is not an
19 * option. Keep in mind that ID-marking (querying a start ID) is a decent
22 * @file RedBeanPHP/BeanCollection.php
23 * @author Gabor de Mooij and the RedBeanPHP community
27 * copyright (c) G.J.G.T. (Gabor) de Mooij and the RedBeanPHP Community
28 * This source file is subject to the BSD/GPLv2 License that is bundled
29 * with this source code in the file license.txt.
36 protected $cursor = NULL;
41 protected $repository = NULL;
46 protected $type = NULL;
49 * Constructor, creates a new instance of the BeanCollection.
51 * @param string $type type of beans in this collection
52 * @param Repository $repository repository to use to generate bean objects
53 * @param Cursor $cursor cursor object to use
57 public function __construct( $type, Repository $repository, Cursor $cursor )
60 $this->cursor = $cursor;
61 $this->repository = $repository;
65 * Returns the next bean in the collection.
66 * If called the first time, this will return the first bean in the collection.
67 * If there are no more beans left in the collection, this method
70 * @return OODBBean|NULL
72 public function next()
74 $row = $this->cursor->getNextItem();
76 $beans = $this->repository->convertToBeans( $this->type, array( $row ) );
77 $bean = array_shift( $beans );
84 * Closes the underlying cursor (needed for some databases).
88 public function close()
90 $this->cursor->close();