PHPackages                             klsoft/yii3-datareader-doctrine - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Database &amp; ORM](/categories/database)
4. /
5. klsoft/yii3-datareader-doctrine

ActiveLibrary[Database &amp; ORM](/categories/database)

klsoft/yii3-datareader-doctrine
===============================

The package provides a Yii 3 data reader that uses the Doctrine ORM

1.0.1(1mo ago)081MITPHP

Since Mar 11Pushed 1mo agoCompare

[ Source](https://github.com/klsoft-web/yii3-datareader-doctrine)[ Packagist](https://packagist.org/packages/klsoft/yii3-datareader-doctrine)[ Docs](https://github.com/klsoft-web/yii3-datareader-doctrine)[ RSS](/packages/klsoft-yii3-datareader-doctrine/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (1)Dependencies (4)Versions (3)Used By (1)

YII3-DATAREADER-DOCTRINE
========================

[](#yii3-datareader-doctrine)

The package provides a [Yii 3 data reader](https://github.com/yiisoft/data?tab=readme-ov-file#reading-data) that uses the [Doctrine ORM](https://www.doctrine-project.org/).

Requirement
-----------

[](#requirement)

- PHP 8.2 or higher.

Installation
------------

[](#installation)

```
composer require yii3-datareader-doctrine
```

How to use
----------

[](#how-to-use)

Example:

```
use App\Data\Entities\User;
use Doctrine\ORM\EntityManagerInterface;
use Yiisoft\Data\Reader\Filter\AndX;
use Klsoft\Yii3DataReaderDoctrine\Filter\ObjectEquals;
use Yiisoft\Data\Reader\Sort;
use Klsoft\Yii3DataReaderDoctrine\DoctrineDataReader;
use Yiisoft\Yii\View\Renderer\WebViewRenderer;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Message\ResponseInterface;

final readonly class UserController
{
    public function __construct(
        private EntityManagerInterface $entityManager,
        private WebViewRenderer        $viewRenderer)
    {
    }

    public function list(ServerRequestInterface $request): ResponseInterface
    {
        return $this->viewRenderer->render(
            __DIR__ . '/list_template',
            [
                'dataReader' => (new DoctrineDataReader(
                    $this->entityManager,
                    User::class,
                    ['id', 'name', 'email']))
                    ->withFilter(new AndX(
                            new ObjectEquals('id', 1)
                        )
                    )
                    ->withOffset(0)
                    ->withLimit(20)
                    ->withSort(Sort::any()
                    ->withOrder(['id' => 'asc']))
            ]
        );
    }
}
```

Example of using the DoctrineDataReader with the GridView from the [yiisoft/yii-dataview](https://github.com/yiisoft/yii-dataview) package

UserController.php:

```
use App\Data\Entities\User;
use Doctrine\ORM\EntityManagerInterface;
use Yiisoft\Data\Reader\Sort;
use Klsoft\Yii3DataReaderDoctrine\DoctrineDataReader;
use Yiisoft\Yii\View\Renderer\WebViewRenderer;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Message\ResponseInterface;

final readonly class UserController
{
    public function __construct(
        private EntityManagerInterface $entityManager,
        private WebViewRenderer        $viewRenderer)
    {
    }

    public function list(ServerRequestInterface $request): ResponseInterface
    {
        return $this->viewRenderer->render(
            __DIR__ . '/list_template',
            [
                'dataReader' => (new DoctrineDataReader(
                    $this->entityManager,
                    User::class,
                    ['id', 'name', 'email']))
                    ->withSort(Sort::any(['id', 'name'])
                    ->withOrder(['name' => 'asc']))
            ]
        );
    }
}
```

list\_template.php:

```
