PHP code example of klsoft / yii3-datareader-doctrine

1. Go to this page and download the library: Download klsoft/yii3-datareader-doctrine library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

klsoft / yii3-datareader-doctrine example snippets


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']))
            ]
        );
    }
}

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']))
            ]
        );
    }
}



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');