| 1 |
Lazy Collections |
| 2 |
================ |
| 3 |
|
| 4 |
To create a lazy collection you can extend the |
| 5 |
``Doctrine\Common\Collections\AbstractLazyCollection`` class |
| 6 |
and define the ``doInitialize`` method. Here is an example where |
| 7 |
we lazily query the database for a collection of user records: |
| 8 |
|
| 9 |
.. code-block:: php |
| 10 |
use Doctrine\DBAL\Connection; |
| 11 |
|
| 12 |
class UsersLazyCollection extends AbstractLazyCollection |
| 13 |
{ |
| 14 |
/** @var Connection */ |
| 15 |
private $connection; |
| 16 |
|
| 17 |
public function __construct(Connection $connection) |
| 18 |
{ |
| 19 |
$this->connection = $connection; |
| 20 |
} |
| 21 |
|
| 22 |
protected function doInitialize() : void |
| 23 |
{ |
| 24 |
$this->collection = $this->connection->fetchAll('SELECT * FROM users'); |
| 25 |
} |
| 26 |
} |