klsoft/yii3-datareader-doctrine
最新稳定版本:1.0.1
Composer 安装命令:
composer require klsoft/yii3-datareader-doctrine
包简介
The package provides a Yii 3 data reader that uses the Doctrine ORM
README 文档
README
The package provides a Yii 3 data reader that uses the Doctrine ORM.
Requirement
- PHP 8.2 or higher.
Installation
composer require yii3-datareader-doctrine
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 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:
<?php declare(strict_types=1); use Yiisoft\View\WebView; use Yiisoft\Yii\DataView\GridView\Column\DataColumn; use Yiisoft\Yii\DataView\GridView\GridView; use Yiisoft\Yii\DataView\YiiRouter\UrlParameterProvider; use Yiisoft\Yii\DataView\YiiRouter\UrlCreator; use Yiisoft\Router\CurrentRoute; use Yiisoft\Router\UrlGeneratorInterface; use Yiisoft\Data\Paginator\OffsetPaginator; use Yiisoft\Data\Reader\DataReaderInterface; /** * @var WebView $this * @var CurrentRoute $currentRoute * @var UrlGeneratorInterface $urlGenerator * @var DataReaderInterface $dataReader */ $this->setTitle('Users'); ?> <h1>Users</h1> <?= GridView::widget() ->dataReader((new OffsetPaginator($dataReader))->withPageSize(20)) ->urlParameterProvider(new UrlParameterProvider($currentRoute)) ->urlCreator(new UrlCreator($urlGenerator)) ->columns( new DataColumn( property: 'id', filter: true ), new DataColumn( property: 'name', filter: true ), new DataColumn( property: 'email', filter: true ) ) ?>
统计信息
- 总下载量: 26
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 5
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2026-03-11