lazy-collections.rst 740 B

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